0
我寫了一個循環:如何將這樣的公式轉換爲循環?
for(int i = 0; i<=5;i++){
Double endingBalance = savingsAmount1 * (1+ (monthlyInterestRate1*i));
以下計算,但是環不輸出正確的價值觀。 savingsAmount1和monthlyInterestRate1都是用戶輸入值。有關如何正確工作的任何想法?我是否應該提示輸入數月和循環月?
Double endingBalance = savingsAmount1 * (1+ (monthlyInterestRate1*i));
double firstMonthEndingBalance1 = savingsAmount1 * (1 + monthlyInterestRate1);
double secondMonthEndingBalance1 = (savingsAmount1 + firstMonthEndingBalance1) * (1 + monthlyInterestRate1);
double thirdMonthEndingBalance1 = (savingsAmount1 + secondMonthEndingBalance1) * (1 + monthlyInterestRate1);
double fourthMonthEndingBalance1 = (savingsAmount1 + thirdMonthEndingBalance1) * (1 + monthlyInterestRate1);
double fifthMonthEndingBalance1 = (savingsAmount1 + fourthMonthEndingBalance1) * (1 + monthlyInterestRate1);
double sixthMonthEndingBalance1 = (savingsAmount1 + fifthMonthEndingBalance1) * (1 + monthlyInterestRate1)
您需要在循環之外的變量來追蹤複合興趣。現在你的本地變量正在被重新創建,每次你的計數器增加。 – DanK