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

我的主要问题是我不知道如何让它不断地将兴趣添加到总数中。我不确定我是否需要使用不同的循环或什么。任何帮助,将不胜感激。

4

2 回答 2

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

你没有改变Invest每一年的价值,所以它不是复利。就像您每年从帐户中获得 0.18 美元的利息一样。

更改TotalInvest.

于 2012-11-27T23:11:49.560 回答
0

您需要将投资利息添加到您的总额中:

        Total = Invest;

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

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

                System.out.println("Value after year " + count + ": " + Total);
                count++;
            }       
于 2012-11-27T23:21:26.103 回答