这个方法程序很疯狂.....需要帮助

This method program is acting wild.....need assistance please

好的,我正在研究的这个 java 程序应该计算 30 年的投资价值。它询问用户他们的起始投资在哪里以及百分比率(以小数形式)。我以为我已经弄明白了,但我的程序返回了一些荒谬的值。有人可以看看我的代码并告诉我我做错了什么吗?

这些是提供给我的示例输出

投资金额是多少? 1000

年利率是多少? .09

Years   Future Value
-----   ------------
1       93.81
2       96.41
...
29      467.25
30      730.58

我的产出正在返回数十亿和数万亿美元的价值……简直是疯狂的东西。我得到的公式是

futureValue = investmentAmmount * (1 + monthlyInterestRate)^numberOfYears*12

这是我的程序的代码

import java.util.Scanner;

 import java.text.NumberFormat;
 import java.util.Locale;
 import java.text.DecimalFormat;
 public class InvestmentValue
 {
      public static void main(String[] args)
      {
         Scanner input = new Scanner(System.in);

         NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);

         double investmentAmmnt;
         // monthly interest rate
         double mri; 
         int years;

         System.out.print("What is the ammount invested? ");
         investmentAmmnt = input.nextDouble();

         System.out.print("What is the annual interest rate? ");
         mri = input.nextDouble();

         futureInvestmentValue(investmentAmmnt, mri, 30);
         }

         public static double futureInvestmentValue(double investmentAmmnt, double mri, int years)
         {
            NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);

            System.out.println("The amount invested: " + (df.format(investmentAmmnt)));
            System.out.println("Annual interest rate: " + mri);
            System.out.println("Years \t \t Future Value");
            for (int i = 1; i <= years * 12; i++){
               investmentAmmnt = investmentAmmnt * Math.pow(1 + (mri / 12),(years * 12));
            if (i % 12 == 0){
               System.out.println(i / 12 + "\t\t" + (df.format(investmentAmmnt)));
               }
               }
            return investmentAmmnt;
        }
   }  

工作原理如下:

  public static double futureInvestmentValue(final double investmentAmmnt, double mri, int years)
  {
    NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
    double amount = investmentAmmnt;

    System.out.println("The amount invested: " + (df.format(investmentAmmnt)));
    System.out.println("Annual interest rate: " + mri);
    System.out.println("Years \t \t Future Value");
    for (int i = 1; i <= years ; i++){
      amount = investmentAmmnt * Math.pow(1 + (mri /100),(i ));
    System.out.println(i  + "\t\t" + (df.format(amount)));
    }
    return amount;
  }

你的代码问题很多...

  1. 你在循环中使用 Math.pow() 增加数量 - 这就是为什么它变得疯狂。
  2. 你的循环迭代了 years * 12(月?)
  3. 你不需要进行疯狂的 *12 和 /12 计算。

--> 输出

Years Future Value 1 ,030.00 2 ,060.90 3 ,092.73 4 ,125.51 5 ,159.27 6 ,194.05 7 ,229.87 8 ,266.77 9 ,304.77 10 ,343.92 11 ,384.23 12 ,425.76 13 ,468.53 14 ,512.59 15 ,557.97 16 ,604.71 17 ,652.85 18 ,702.43 19 ,753.51 20 ,806.11 21 ,860.29 22 ,916.10 23 ,973.59 24 ,032.79 25 ,093.78 26 ,156.59 27 ,221.29 28 ,287.93 29 ,356.57 30 ,427.26

问题是公式 futureValue = investmentAmmount * (1 + monthlyInterestRate)^numberOfYears*12 计算了未来任意年数的投资价值。问题是你的 for 循环计算的比它需要的多。该公式只需要完成一次。您的函数 futureInvestmentValue 不应包含 for 循环。