2012-11-27 229 views
0

我正在制定計算存款憑證應計基本利息的計劃。該計劃要求投入的金額和期限(長達五年)。取決於他們的學期有多少年,是決定贏得多少利息的原因。我使用if/else語句來確定感興趣的比率。然後我使用循環打印出每年年底帳戶中有多少錢。我的問題是,當我運行該程序時,這筆錢不計算在內。計算存款憑證的利息

這是整個代碼。

import java.util.Scanner; 

public class CDCalc 
{ 
    public static void main(String args[]) 
     { 
      int Count = 0; 
      double Rate = 0; 
      double Total = 0; 


      Scanner userInput = new Scanner(System.in); 

      System.out.println("How much money do you want to invest?"); 
      int Invest = userInput.nextInt(); 

      System.out.println("How many years will your term be?"); 
      int Term = userInput.nextInt(); 

      System.out.println("Investing: " + Invest); 
      System.out.println("  Term: " + Term); 

      if (Term <= 1) 
      { 
      Rate = .3; 
      } 

      else if (Term <= 2) 
      { 
      Rate = .45; 
      } 

      else if (Term <= 3) 
      { 
      Rate = .95; 
      } 

      else if (Term <= 4) 
      { 
      Rate = 1.5; 
      } 

      else if (Term <= 5) 
      { 
      Rate = 1.8; 
      } 


      int count = 1; 
        while(count <= 5) 
       { 

        Total = Invest + (Invest * (Rate)/(100.0)); 

        System.out.println("Value after year " + count + ": " + Total); 
        count++; 
       }  
     } 
} 

這裏是我用10美元的投資得到的結果,只是爲了保持簡單和5年的投資。

How much money do you want to invest? 
10 
How many years will your term be? 
5 
Investing: 10 
    Term: 5 
Value after year 1: 10.18 
Value after year 2: 10.18 
Value after year 3: 10.18 
Value after year 4: 10.18 
Value after year 5: 10.18 

我的主要問題是我不知道如何使它不斷地添加intrest到總數。我不確定是否需要使用不同的迴路或什麼。任何幫助,將不勝感激。

+0

這是很好的做法,遵循良好的命名約定。即你的變量不應該以大寫開頭:) – WilliamShatner

回答

1
Total = Invest + (Invest * (Rate)/(100.0)); 

您不會每年更改Invest的值,所以不會混合。這就像你每年從賬戶中退出18美元的利息。

更改Total對於Invest

+0

非常感謝!絕對有它的工作。 –

0

您需要的投資興趣添加到您的總:

 Total = Invest; 

     int count = 1; 
       while(count <= 5) 
      { 

       Total = Total + (Invest * (Rate)/(100.0)); 

       System.out.println("Value after year " + count + ": " + Total); 
       count++; 
      }