【问题标题】:calculating interest on a certificate of deposit计算存款证明的利息
【发布时间】:2012-11-15 16:51:41
【问题描述】:

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

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

【问题讨论】:

  • 遵循良好的命名约定是一种很好的做法。即你的变量不应该以大写开头:)

标签: java


【解决方案1】:
  Total = Invest + (Invest * (Rate) / (100.0));

您不会每年更改Invest 的值,因此它不是复利。就像您每年从帐户中获得 0.18 美元的利息一样。

Total 更改为Invest

【讨论】:

    【解决方案2】:

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

            Total = Invest;
    
            int count = 1;
                    while(count <= 5)
                {
    
                    Total =  Total +  (Invest * (Rate) / (100.0));
    
                    System.out.println("Value after year " + count + ": " + Total);
                    count++;
                }       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多