首页 > 解决方案 > 投资程序,没有给出最终结果所需的正确计算

问题描述

示例输出 1:

输入您的姓名:鲍勃·约翰逊

年龄:20

每月供款:300 美元

年回报率(%):8

投资年限:10

开始投资年龄:20

提款前的年数:40

提款时的总投资为:596,219.72 美元

示例输出 2:

输入您的姓名:瑞奇·特罗特

年龄:20

每月供款:300 美元

年回报率(%):8

投资年限:30

开始投资年龄:30

提款前的年数:40

提款时的总投资为:443,548.84 美元

代码仅给出第一个和第二个周期的正确值。最终值为 2.49E163

class Main 
{

  public static void main(String[] args) 
  {

    Scanner scan = new Scanner(System.in);
      System.out.print("Enter your name: ");
      String name = scan.nextLine();
      System.out.print("Age: ");
      double age = scan.nextInt();
      System.out.print("Monthly Contribution: ");
      double month = scan.nextDouble();
      System.out.print("Anual Rate of Return (%): ");
      double rate = scan.nextInt();
      rate = (rate/100.0);
      System.out.print("Years of Investing: ");
      double years = scan.nextInt();
      System.out.print("Start of Investing Age: ");
      double start = scan.nextInt();
      System.out.print("Years until withdrawing Money: "); 
      double yearsuntil = scan.nextInt();
      double cont = month;
      int year = 12;
      double fv = 0.0;
      for  (int i = 0; i <= yearsuntil; i++)
      {
        for (int j = 0; j <= year; j++)
        {
          //Future Value = Monthly Contribution * Annual Rate of Return / 12 + Monthly Contribution
          fv = ((cont * .08)/12) + cont;
          cont += fv;
        }
      }
      NumberFormat total = NumberFormat.getCurrencyInstance(); 
    System.out.println("Total Investments at Withdraw time will be: " + total.format(fv));

  }

}

标签: java

解决方案


推荐阅读