即使它正在编译,我对这个程序也有点麻烦。它说我必须添加一个主要方法,但是,我这样做了,我最终得到了21个错误。有人请帮帮我..
创建一个SavingsAccount类。使用静态类变量存储每个保存器的annualInterestRate。该类的每个对象都包含一个私有实例变量savingsBalance,表示存储器当前具有的存储量。提供方法calculateMonthlyInterest通过将balanceInterestRate乘以12来计算月利息;这种兴趣应该加到savingsBalance。提供一个静态方法modifyInterestRate,它将annualInterestRate设置为一个新值。编写驱动程序来测试SavingsAccount类。实例化两个不同的savingsAccount对象,saver1和saver2,余额分别为$ 2000.00和$ 4000.00。将annualInterestRate设置为3%,然后计算每月利息并打印每个储户的新余额。然后将annualInterestRate设置为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();
}
}
答案 0 :(得分:0)
您的代码运作完美。请使用SavingsAccount.java
和compile
使用javac SavingsAccount.java
保存为run
和java Driver
。
我compile
和run
您的代码和output
就是这个
答案 1 :(得分:0)
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;
}
}
答案 2 :(得分:0)
您可以尝试以下代码。
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 :(得分:-1)
将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();
}
}