我創建了一種計算並顯示基於年數的簡單興趣值的方法。我想告訴所有的年,所以例如,如果年是5,我想說明年1,2,3,4的值,5顯示for循環的所有迭代
這是到目前爲止我的代碼
public static String numbers(String method, double principal, double rate, double years) {
double simpleInterest = principal + (principal * (rate/100) * years);
String interest = "";
for (double digit = 1; digit <= years; digit++) {
if (method.equals("Simple")) {
interest = "Principal: $" + principal + ',' + " Rate: " + rate + "\n" + "Year Simple Interest Amount\n"
+ digit + "-->$" + simpleInterest;
}
}
return interest;
}
public static void main(String[] args) {
System.out.print(numbers("Simple", 5000, 5, 5));
}
我輸出
Principal: $5000.0, Rate: 5.0
Year Simple Interest Amount
5.0-->$6250.0
但我想它也顯示了往年一樣
Principal: $5000.0, Rate: 5.0
Year Simple Interest Amount
1.0-->$5250.0
2.0-->$5500.0
3.0-->$5750.0
4.0-->$6000.0
5.0-->$6250.0
什麼我需要做的,屏幕顯示T他前幾年?
那你試試?我的意思是,不會說謊,你已經編碼,只是沒有打印。 – Fallenreaper
首先你沒有計算利息,但是如果沒有付款,總欠款。所以我假設你在下面將'interest'重命名爲'total'。你可以在計算循環中放置一個打印語句,或者你必須將年度總計作爲一個列表返回,比如'totals = []'在循環前創建一個空列表,然後'totals.append(total) '循環內,最後'返回總計' – roadrunner66