作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个程序,该程序将计算存款单上应计的基本利息。该计划要求提供投资金额和期限(最长五年)。根据他们的期限有多少年,决定了赚取多少利息。我使用 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
我的主要问题是我不知道如何让它不断地将兴趣添加到总数中。我不确定是否需要使用不同的循环或什么。任何帮助,将不胜感激。
最佳答案
Total = Invest + (Invest * (Rate) / (100.0));
您不会更改每年的投资
值(value),因此它不会复利。这就像您每年获得 0.18 美元的利息,从账户中退休。
更改投资
的总计
。
关于java - 计算存款单的利息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13594881/
我是一名优秀的程序员,十分优秀!