【问题标题】:JAVA Looping a formulaJAVA循环公式
【发布时间】:2017-11-18 04:52:17
【问题描述】:

首先,我是一个相对较新的程序员,因为它与 JAVA 相关。我已经搜索了很多东西,但可能我没有找到正确的东西。我基本上试图从第一个“年”到第 10 个“年”列出一个起始金额,乘以百分比,然后添加到起始金额。然后,我想拿新的金额,一遍又一遍地做同样的事情,直到达到 10。或者,甚至是指定的总数,例如:2000。

public static void main(String[] args)
{
    double startAmount = 1000;
    double ratePercentage = 0.05;
    double newAmount;


    for (int i = 0; i <= 10; i++){
        newAmount = startAmount + (startAmount * ratePercentage);
        System.out.println(i + " " + newAmount);

    }



}

【问题讨论】:

标签: java loops formula


【解决方案1】:

我不能 100% 确定您要达到的目标。但是假设你正在尝试做某种复利。这应该会有所帮助。

public static void main(String[] args)
{
    double startAmount = 1000;
    double ratePercentage = 0.05;
    double newAmount;

    newAmount = startAmount;

    for (int i = 0; i <= 10; i++){
        newAmount = newAmount + (newAmount * ratePercentage);
        System.out.println(i + " " + newAmount);

    }



}

【讨论】:

    【解决方案2】:

    试试这个

    public static void main(String[] args)
    {
    double startAmount = 1000;
    double ratePercentage = 0.05;
    double newAmount;
    
    
    for (int i = 0; i < 10; i++){
        newAmount = startAmount + (startAmount * ratePercentage);
        System.out.println(i + " " + newAmount);
        startAmount=newAmount;
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多