我正在开发一个程序,该程序将计算存款证明上应计的基本利息。该计划要求提供投资金额和期限(最长五年)。取决于他们的任期是多少年,决定了赚取多少利息。我使用 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
我的主要问题是我不知道如何让它不断地将兴趣添加到总数中。我不确定我是否需要使用不同的循环或什么。任何帮助,将不胜感激。