2012-11-27 191 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++; 
      }