gpt4 book ai didi

java - 计算存款单的利息

转载 作者:行者123 更新时间:2023-12-01 15:01:23 25 4
gpt4 key购买 nike

我正在开发一个程序,该程序将计算存款单上应计的基本利息。该计划要求提供投资金额和期限(最长五年)。根据他们的期限有多少年,决定了赚取多少利息。我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com