-1

问题如下:

(储蓄账户类)创建类 SavingsAccount。使用静态变量年利率来存储所有账户持有人的年利率。该类的每个对象都包含一个私有实例变量 SavingBalance,指示储蓄者当前的存款金额。提供方法 calculateMonthlyInterest 以通过将 SavingBalance 乘以 AnnualInterestRate 除以 12 来计算每月利息——这个利息应该添加到 Savings-Balance 中。提供一个静态方法 modifyInterestRate 将年利率设置为新值。编写一个程序来测试类 SavingsAccount。实例化两个 SavingAccount 对象,saver1 和 saver2,余额分别为 $2000.00 和 $3000.00。将年利率设置为 4%,然后计算 12 个月的每个月的利息并打印两个储户的新余额。接下来,将年利率设置为 5%,计算下个月的利息并打印两个储蓄者的新余额。

我解决了这一切,但余额并没有增加——它保持不变。它应该随着年利率的每次变化而增加(至少,这是我所理解的)。

class SavingsAccount
{
    static double annualInterestRate;
    private double savingsBalance;

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

    public double calculateMonthlyInterest()
    {
        return (savingsBalance*annualInterestRate)/12;
    }

    public static void modifyInterestRate(double rate)
    {
        annualInterestRate = rate;
    }


    public static double getannualInterestRate(){return annualInterestRate;}

    public double getsavingsBalance(){return savingsBalance;}

}

public class SavingsTest
{
    public static void main(String args[])
    {
        SavingsAccount saver1 = new SavingsAccount(2000.0);
        SavingsAccount saver2 = new SavingsAccount(3000.0);

        SavingsAccount.modifyInterestRate(4);

        System.out.printf("Balance for Saver1 = %.2f\nBalance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance()+saver1.calculateMonthlyInterest(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());

        SavingsAccount.modifyInterestRate(5);

        System.out.printf("New Balance for Saver1 = %.2f\nNew Balance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());
    }
}
4

4 回答 4

1

您没有修改savingsBalance此代码中的值。

于 2012-04-10T20:03:13.837 回答
0
public double addMonthlyInterest()     {    
  savingsBalance += (savingsBalance*annualInterestRate)/12;
  return savingBalance;     
}  

这将返回新的余额和“增量”

于 2012-04-10T20:09:44.827 回答
0

在您的第一个打印语句中,对于第一个参数,您在计算每月利息并将它们加在一起之后计算余额,并且没有将新值设置为类成员 SavingBalance 的值。

在您的第二个打印语句中,由于您没有通过设置器将计算值设置回类成员,因此它只是打印出实例化类的原始值。

附带说明,不要在打印语句中进行内联计算。它们令人困惑,任何人都不容易阅读。一个好的方法是在方法中初始化本地成员,并使用它们进行计算和打印本地成员。

于 2012-04-10T20:11:01.403 回答
0

只是想分享我的答案。

公共类 SavingsAccount {

private static float annualInterestRate = 0f;
private float savingsBalance;

public SavingsAccount(float balance) {
    savingsBalance = balance;
}

public static void setAnnualInterestRate(float t) {
    if (t >= 0 && t <= 1)
        annualInterestRate = t;
    else
        throw new IllegalArgumentException("Annual interest rate should be between 0 and 1");
}

private float calculateMonthlyInterest() {
    return savingsBalance * annualInterestRate / 12;
}

public float getSavingsBalance() {
    return savingsBalance + calculateMonthlyInterest();
}

public float getAnnualInterestRate(){
    return annualInterestRate;
}

public String toString() {
    return String.format("Balance: %.2f", getSavingsBalance());
}

}

主要是

    SavingsAccount s1 = new SavingsAccount(2000);
    SavingsAccount s2 = new SavingsAccount(3000);
    SavingsAccount.setAnnualInterestRate(0.04f);

    System.out.println("S1: " + s1);
    System.out.println("S2: " + s2);

    SavingsAccount.setAnnualInterestRate(0.05f);
    System.out.println("S1: " + s1);
    System.out.println("S2: " + s2);
于 2015-07-08T04:21:09.250 回答