2016-02-26 157 views
1

我創建了一種計算並顯示基於年數的簡單興趣值的方法。我想告訴所有的年,所以例如,如果年是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他前幾年?

+0

那你試試?我的意思是,不會說謊,你已經編碼,只是沒有打印。 – Fallenreaper

+0

首先你沒有計算利息,但是如果沒有付款,總欠款。所以我假設你在下面將'interest'重命名爲'total'。你可以在計算循環中放置一個打印語句,或者你必須將年度總計作爲一個列表返回,比如'totals = []'在循環前創建一個空列表,然後'totals.append(total) '循環內,最後'返回總計' – roadrunner66

回答

0

您必須計算simpleInterest每年的simpleInterest = principal + (principal * (rate/100) * digit);這裏digit代表year

並且還使用+=運算符將結果連接到interestreturn它。 您也可以使用字符串concat()方法 以下是此方法的語法:public String concat(String s)。 你可以使用它像這樣:interest = interest.concat(digit + "-->$" + simpleInterest + "\n");

public static String numbers(String method, double principal, double rate, double years) { 

     double simpleInterest = principal + (principal * (rate/100) * years); 
     String interest = ""; 
     System.out.println("Principal: $" + principal + ',' + " Rate: " + rate + "\n"+ "Year Simple Interest Amount\n"); 
     for (double digit = 1; digit <= years; digit++) { 
      if (method.equals("Simple")) { 
       simpleInterest = principal + (principal * (rate/100) * digit); 
       interest += digit + "-->$" + simpleInterest + "\n"; 
      } 
     } 
     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 

1.0-->$5250.0 
2.0-->$5500.0 
3.0-->$5750.0 
4.0-->$6000.0 
5.0-->$6250.0 
+1

謝謝。 + =運算符解決我的問題 –

0
if (method.equals("Simple")) { 
      interest = "Principal: $" + principal + ',' + " Rate: " + rate + "\n" + "Year Simple Interest Amount\n" 
        + digit + "-->$" + simpleInterest; 
     } 
在你上面的代碼

被重新分配interest如果method等於"simple"。因此,返回最後分配的值。

使用+=將新結果連接到interest或僅返回String[],其中包含每個結果作爲單個元素,然後遍歷它。

0

公共靜態字符串編號(字符串方法,雙重委託,雙倍速率,雙年) {

 String interest = ""; 
     for (double digit = 1; digit <= years; digit++) 
     { 
      if (method.equals(Simple)) 
      { 
       double simpleInterest = principal + (principal * (rate/100) * digit); 
       interest += "Principal: $" + principal + ',' + " Rate: " + rate + "\n" + "Year Simple Interest Amount\n" 
         + digit + "-->$" + simpleInterest;     
      } 
     } 
     return interest; 
    } 
+0

您需要每年使用「+ =」附加到字符串興趣。否則,它會將其更改爲最新的。而且你需要在double simpleInterest中改變一年,否則simpleInterest總是$ 6,250。 – David