【问题标题】:SavingsAccount Program储蓄账户计划
【发布时间】:2016-03-28 03:55:20
【问题描述】:

我在使用这个程序时遇到了一点问题,即使它已编译。它说我必须添加一个主要方法,但是,我这样做了,我最终得到了 21 个错误。有人请帮助我..

创建一个类 SavingsAccount。使用静态类变量来存储每个储蓄者的年利率。该类的每个对象都包含一个私有实例变量 SavingBalance,指示储蓄者当前的存款金额。提供方法calculateMonthlyInterest,通过将余额乘以annualInterestRate除以12来计算每月利息;这个利息应该加到储蓄余额中。提供一个静态方法 modifyInterestRate 将年利率设置为新值。编写一个驱动程序来测试类 SavingsAccount。实例化两个不同的 SavingAccount 对象 saver1 和 saver2,余额分别为 $2000.00 和 $4000.00。将年利率设置为 3%,然后计算每月利息并打印每个储蓄者的新余额。然后将年利率设置为 5% 并计算下个月的利息并打印每个储蓄者的新余额。

import java.util.Scanner;
public class SavingsAccount{

    private static double annualInterestRate;
    private double savingsBalance;

    public SavingsAccount()
    {
        savingsBalance = 0;
        annualInterestRate = 0;
    }

    public SavingsAccount(double balance)
    {
        savingsBalance = balance;
        annualInterestRate = 0;
    }

    public void calculateMonthlyInterest()
    {
        System.out.println("Current savings balance: " + savingsBalance);
        double monthlyInterest;
        monthlyInterest = (savingsBalance * annualInterestRate)/12;
        savingsBalance = monthlyInterest;
        System.out.println("New savings balance: " + savingsBalance);
    }

    public double getBalance()
    {
        return savingsBalance;
    }
    public static void modifyInterestRate(double newInterestRate)
    {
        annualInterestRate = newInterestRate;
    }
}
class Driver
{
    public static void main(String[] args)
    {
        SavingsAccount saver1 = new SavingsAccount(2000);
        SavingsAccount saver2 = new SavingsAccount(4000);
        saver1.modifyInterestRate(.03);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.03);
        saver2.calculateMonthlyInterest();
        saver1.modifyInterestRate(.05);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.05);
        saver2.calculateMonthlyInterest();
    }
}

【问题讨论】:

  • 能否请您添加错误堆栈跟踪?
  • 没有错误,可以编译。但是当它运行时它说错误没有主要方法或类似的东西
  • 你好@Leonardo 我检查你的代码。它工作得很好,你可以在我给出的答案中看到输出。
  • 哦,好吧,我完全忘了为驱动程序创建一个单独的文件。我是否必须将我的驱动程序类复制并粘贴到另一个 java 程序中并单独保存?
  • 不,这两个类都保存为 SavingsAccount.java 不单独的文件并检查我的答案

标签: java oop object


【解决方案1】:

您的代码运行良好。请使用javac SavingsAccount.javarun 使用java Driver 保存为SavingsAccount.javacompile

compilerun 你的代码和output 是这个

【讨论】:

    【解决方案2】:

    你可以试试下面的代码。

    package savingsaccount;
    
    
    public class SavingsAccount{
    
        private static double annualInterestRate;
        private double savingsBalance;
    
        public SavingsAccount()
        {
            savingsBalance = 0;
            annualInterestRate = 0;
        }
    
        public SavingsAccount(double balance)
        {
            savingsBalance = balance;
            annualInterestRate = 0;
        }
    
        public void calculateMonthlyInterest()
        {
            System.out.println("Current savings balance: " + savingsBalance);
            double monthlyInterest;
            monthlyInterest = (savingsBalance * annualInterestRate)/12;
            savingsBalance = monthlyInterest;
            System.out.println("New savings balance: " + savingsBalance);
        }
    
        public double getBalance()
        {
            return savingsBalance;
        }
        public static void modifyInterestRate(double newInterestRate)
        {
            annualInterestRate = newInterestRate;
        }
    
    
        public static void main(String[] args)
        {
            SavingsAccount saver1 = new SavingsAccount(2000);
            SavingsAccount saver2 = new SavingsAccount(4000);
            SavingsAccount.modifyInterestRate(.03);
            saver1.calculateMonthlyInterest();
            SavingsAccount.modifyInterestRate(.03);
            saver2.calculateMonthlyInterest();
            SavingsAccount.modifyInterestRate(.05);
            saver1.calculateMonthlyInterest();
            SavingsAccount.modifyInterestRate(.05);
            saver2.calculateMonthlyInterest();
        }
    }
    

    【讨论】:

      【解决方案3】:

      calculateMonthlyInterest() 中,您需要将每月利息添加到储蓄余额中,而不是重置余额。即

          savingsBalance = monthlyInterest;
      

      应该是:

          savingsBalance += monthlyInterest;
      

      这个更正后的SavingsAccount 类应该是:

      public class SavingsAccount{
      
          private static double annualInterestRate;
          private double savingsBalance;
      
          public SavingsAccount()
          {
              savingsBalance = 0;
              annualInterestRate = 0;
          }
      
          public SavingsAccount(double balance)
          {
              savingsBalance = balance;
              annualInterestRate = 0;
          }
      
          public void calculateMonthlyInterest()
          {
              System.out.println("Current savings balance: " + savingsBalance);
              double monthlyInterest;
              monthlyInterest = (savingsBalance * annualInterestRate)/12;
              savingsBalance += monthlyInterest;
              System.out.println("New savings balance: " + savingsBalance);
          }
      
          public double getBalance()
          {
              return savingsBalance;
          }
          public static void modifyInterestRate(double newInterestRate)
          {
              annualInterestRate = newInterestRate;
          }
      }
      

      【讨论】:

      • savingsBalance 应该对每月的利息求和,这样才会出现正确的答案;
      【解决方案4】:

      将 main 方法放在 SavingsAccount 类中。

      这样,

      public class SavingsAccount{
      
         private static double annualInterestRate;
          private double savingsBalance;
      
          public SavingsAccount()
          {
              savingsBalance = 0;
              annualInterestRate = 0;
          }
      
          public SavingsAccount(double balance)
          {
              savingsBalance = balance;
              annualInterestRate = 0;
          }
      
          public void calculateMonthlyInterest()
          {
              System.out.println("Current savings balance: " + savingsBalance);
              double monthlyInterest;
              monthlyInterest = (savingsBalance * annualInterestRate)/12;
              savingsBalance = monthlyInterest;
              System.out.println("New savings balance: " + savingsBalance);
          }
      
          public double getBalance()
          {
              return savingsBalance;
          }
          public static void modifyInterestRate(double newInterestRate)
          {
              annualInterestRate = newInterestRate;
          }
      
          public static void main(String[] args)
          {
              SavingsAccount saver1 = new SavingsAccount(2000);
              SavingsAccount saver2 = new SavingsAccount(4000);
              saver1.modifyInterestRate(.03);
              saver1.calculateMonthlyInterest();
              saver2.modifyInterestRate(.03);
              saver2.calculateMonthlyInterest();
              saver1.modifyInterestRate(.05);
              saver1.calculateMonthlyInterest();
              saver2.modifyInterestRate(.05);
              saver2.calculateMonthlyInterest();
          }
      }
      

      【讨论】:

      • 请回答。无需将 main 方法放入“SavingsAccount 类”中即可完美运行
      • 它仍然无法正常工作就像我不知道你通过将驱动程序类保存在不同的文件中一样
      猜你喜欢
      • 2016-06-24
      • 2023-03-30
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 2020-11-13
      相关资源
      最近更新 更多