我正在制定一个程序来计算存款证所累积的基本利息。该计划要求投入的金额和期限(最多五年)。根据他们的学期多少年,决定了多少利息。我使用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
我的主要问题是我不知道如何让它不断增加总量。我不确定我是否需要使用不同的循环或什么。任何帮助,将不胜感激。
答案 0 :(得分:1)
Total = Invest + (Invest * (Rate) / (100.0));
您每年都不会更改Invest
的值,因此它不会复合。这就像你每年获得18美元的利息,从账户中退休。
更改Total
的{{1}}。
答案 1 :(得分:0)
您需要将投资利息添加到总额中:
Total = Invest;
int count = 1;
while(count <= 5)
{
Total = Total + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}