投资计算器

时间:2014-07-27 16:00:13

标签: java

嘿,到目前为止,我已经建立了这个代码,以便在x年的过程中进行复利,但我不知道如何在基金中加入每年的" x美元金额。&# 34;因此,如果我将我的变量设为70,000美元,它将在x年内复利,难点在于每年增加美元数量,以及投入的初始金额。"代码发布在

之下
 import java.util.*;
//Feel Free To Steal My Code  Sincerely Minotaur,

public class Investing {

public static void main (String[] args){

        Scanner input1 = new Scanner(System.in);

        double amount;

        double principal;

        double rate;

        System.out.println("Please Enter The Principal");
        principal = input1.nextDouble();

        System.out.println("Please Enter The Rate Of Interest In Decimal Form");
        rate = input1.nextDouble();

        System.out.println("Please Enter The Number Of Investment Years");
        Scanner input2 = new Scanner (System.in);
        int g = input2.nextInt();

        for(int year = 1; year <= g; year++){
            amount = principal * Math.pow(1 + rate, year);
            System.out.println(year + "  " + amount);

        }           
    }       
}

2 个答案:

答案 0 :(得分:1)

不是从校长重新计算价值,而是从上一年开始计算:

amount = principal;
for(int year = 1; year <= g; year++){
    amount *= 1 + rate;
    System.out.println(year + "  " + amount);
}

然后,为了每年添加特定金额,只需添加金额

amount = principal;
for(int year = 1; year <= g; year++){
    amount *= 1 + rate;
    amount+=<amount to add>;
    System.out.println(year + "  " + amount);
}

答案 1 :(得分:0)

  amount = principal * Math.pow((1 + rate/100),time);

而不是

amount = principal * Math.pow(1 + rate, year);

我用来获取复利的代码:-(优化)

import java.util.Scanner;

public class CompoundInterest {

   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);

      double principal = 0;
      double rate = 0;
      double time = 0;

      double compoundInterest = 0;

      System.out.print("Enter the Principal amount : ");
      principal = input.nextDouble();

      System.out.print("Enter the Rate : ");
      rate = input.nextDouble();

      System.out.print("Enter the Time : ");
      time = input.nextDouble();

      compoundInterest = principal * Math.pow((1 + rate/100),time);

      System.out.println("");
      System.out.println("The Compound Interest is : "
     + compoundInterest);

   }

}