有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我如何计算和显示未来5年每一年的投资价值

我相信我在这里犯了一个数学公式错误,代码似乎是有效的,但是数学结果完全错误了

   public static void main (String [] args) {
       
       Scanner sam = new Scanner(System.in);
       
       System.out.print("enter initial investment: ");
       double principal = sam.nextDouble();
       System.out.print("enter initial rate:  ");
       double Rate = sam.nextDouble();
       System.out.print("Enter the time:  ");
       double time = sam.nextDouble();
       Rate = (1 + (Rate / 100));
       double interest = (principal * Rate);
       for (int i=1; i < time; i++) {
           System.out.printf("year %d \n", i);
           System.out.printf("interest: %f \n", interest);
           double amount = (principal + interest);
           interest = (amount * Rate);
           System.out.printf("value: %f \n", amount);
       }
    }

输出:

enter initial investment: 1000
enter initial rate:  5
Enter the time:  5
year 1 
interest: 1050.000000 
value: 2050.000000 
year 2 
interest: 2152.500000 
value: 3152.500000 
year 3 
interest: 3310.125000 
value: 4310.125000 
year 4 
interest: 4525.631250 
value: 5525.631250

共 (2) 个答案

  1. # 1 楼答案

    问题似乎在于将比率设置为1 + Rate / 100,即比率超过100%。此外,利息没有适当地计入本金

    另外,最好避免在货币算术中使用double

    因此,固定代码可能如下所示:

           Scanner sam = new Scanner(System.in);
           
           System.out.print("enter initial investment: ");
           int principal = sam.nextInt() * 100; // in cents
           System.out.print("enter initial rate:  ");
           int Rate = sam.nextInt(); // percent
           System.out.print("Enter the time:  ");
           int time = sam.nextInt();
           
           int interest = (principal * Rate) / 100;
           for (int i=1; i < time; i++) {
               System.out.printf("year %d \n", i);
               System.out.printf("interest: %s \n", interest / 100.0);
               principal += interest; // adding interest
               interest = (principal * Rate) / 100;
               System.out.printf("value: %s\n", principal / 100.0);
           }
    

    输出

    enter initial investment: 1000
    enter initial rate:  5
    Enter the time:  5
    year 1 
    interest: 50.0 
    value: 1050.0
    year 2 
    interest: 52.5 
    value: 1102.5
    year 3 
    interest: 55.12 
    value: 1157.62
    year 4 
    interest: 57.88 
    value: 1215.5
    
  2. # 2 楼答案

    好吧,多亏了@alex rudenko的回答,我成功地解决了这个问题 '''

       Scanner sam = new Scanner(System.in);
       
       System.out.print("enter initial investment: ");
       double principal = sam.nextDouble();
       System.out.print("enter initial rate:  ");
       double Rate = sam.nextDouble();
       System.out.print("Enter the time:  ");
       double time = sam.nextDouble();
       Rate /= 100.0;
       double interest = (principal * Rate);
       for (int i=1; i < time; i++) {
           System.out.printf("year %d \n", i);
           System.out.printf("interest: %f \n", interest);
           principal  = (principal + interest);
           interest = (principal*Rate); 
           System.out.printf("value: %f \n", principal);
       }
    }
    

    '''

    输出: 输入初始投资:1000 输入初始速率:5 输入时间:5 第一年 利息:50.000000 价值:1050.000000 第二年 利息:52.500000 价值:1102.500000 第三年 利息:55.125000 价值:1157.625000 第四年 利息:57.881250 数值:1215.506250