【问题标题】:How to do an array in a loop如何在循环中做一个数组
【发布时间】:2017-11-26 04:16:14
【问题描述】:

我正在做一个投资计算器。

所以我会做一道数学题,我想在每个数组中循环它,其中会有 12 个。

假设我要投资 1000 美元,回报率为 4.5%。

所以我需要做 1000 * 0.045 + 1000 = 1,045 等于一个月。然后我需要做 1,045 * 0.045 + 1,045 = 1,092 这将等于第二个月,我将如何让它通过这样的循环?

我会使用 for 循环吗?还是?

这就是我目前所拥有的,也许你读了它会变得更好。但是我仍然需要创建一个循环,就像我上面给出的示例一样。

public class SimpleInvestment {

    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) 
    {
        double [] num = new double [11];
        printWelcome();
        double investTotal = getInvestAmount();
        double rateTotal = getRate();

    }

    public static void printWelcome()
    {
        System.out.println("Welcome to the Investment Calulator");
    }

    public static double getInvestAmount()
    {
        double amountInvest;
        System.out.print("Hou much will you be investing?  ");
        amountInvest = input.nextDouble();
        while (amountInvest <= 0)
        {
            System.out.println("Amount must be greater than 0. Try again.");
            System.out.print("How much will you be investing?  ");
            amountInvest = input.nextDouble();
        }
        return amountInvest;
    }

    public static double getRate()
    {
        double rate;
        System.out.print("What will be the rate of return?");
        rate = input.nextDouble();
        while (rate < 0 )
        {
            System.out.println("Rate must be greater than 0. Try again.");
            System.out.print("What will be the rate of return?");
            rate = input.nextDouble();
        }
        return rate;
    }

    public static void calculateInterst( double num[], double investTotal, double rateTotal) 
    {
        double total; 
        rateTotal = rateTotal / 100;
        total = investTotal * rateTotal + investTotal;
    }

}

【问题讨论】:

  • 为什么不做 1000 * 1.045 一个月呢?等等。投资 x 个月或同时仍有投资 $amount 现在获得利息。
  • 如果您希望对您的代码进行审查,请发布here。如果您询问如何重复计算 1000 * 0.045 + 1000 ,请删除所有不相关的代码,如欢迎和用户输入相关代码。见minimal reproducible example

标签: java arrays loops


【解决方案1】:

您可以使用 while 或 for 循环。在这个例子中,我使用了一个 for 循环。

代码中有文档可以引导您完成逻辑。

public static void calculateInterest(double num, double investTotal, double rateTotal) {
    double total; //keep the total outside loop
    rateTotal = rateTotal / 100; //your percent to decimal calculation
    total = investTotal * rateTotal + investTotal; //intial calculation

    for (int i = 1; i < num; i++) {//for loop starting at 1 because we alreay calculated the first

        total += (total * rateTotal);//just calculate the rate of change

    }

    System.out.println(total);//either output it or return it. Whatever you want to do from here.
}

我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您可以使用以下代码: 其中months是以月为单位的投资期限, 投资是投资的金额, rate 是利率。例如0.045

        public static double calculateTotal(int months, double investment, double rate) {
            double total = investment;          
            for (int i=1; i <= months; i++) {
                total = total + (total * rate);
            }           
            return total;
        }
    

    【讨论】:

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