的問題如下:添加靜態屬性來構造屬性
(儲蓄賬戶類)創建類SavingsAccount。使用靜態 變量annualInterestRate來存儲所有 賬戶持有者的年利率。該類的每個對象都包含一個私有實例 變量savingsBalance指示保存者當前擁有的金額爲 。提供方法calculateMonthlyInterest計算 每月利息乘以儲蓄餘額 annualInterestRate除以12-這個利息應該加到 儲蓄餘額。提供一個靜態方法modifyInterestRate,設置 annualInterestRate爲一個新值。編寫一個程序來測試類 SavingsAccount。實例化兩個savingsAccount對象,saver1和 saver2,餘額分別爲$ 2000.00和$ 3000.00。將 年收益率設置爲4%,然後計算12個月的每個 的月利息併爲兩個儲戶打印新的餘額。接下來,將 annualInterestRate設置爲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());
}
}
我不認爲改變利率應該改變帳戶餘額。如果這是你唯一的問題,我認爲這是正確的 – ControlAltDel 2012-04-10 20:01:55
沒有你的代碼實際上修改'savingBalance'變量(除了構造函數)。你必須在某個地方真正改變價值,我不知道你想要做什麼。 – twain249 2012-04-10 20:01:57
順便說一句你的格式/命名約定不好。假設這是hw,你可能會失去分數 – ControlAltDel 2012-04-10 20:03:46