编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示未来投资金额:
未来投资金额 = 投资总额 * (1 + 月利率)^(年数 * 12)
package pack2;
import java.util.Scanner;
public class AccumulatedValue {
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in);) {
System.out.print("Enter investment amount: ");
double investment = input.nextDouble();
System.out.print("Enter annual interest rate in percentage: ");
double annualInterest = input.nextDouble();
System.out.print("Enter number of years: ");
double years = input.nextDouble();
System.out.printf("Accumulated value is $%.2f\n",
accumulatedValue(investment, annualInterest, years));
}
}
//未来投资金额 = 投资总额 * (1 + 月利率)^(年数 * 12)
public static double accumulatedValue(double investment,
double annualInterest, double years) {
double monthlyInterestRate = annualInterest / 1200;
return investment * Math.pow(1 + monthlyInterestRate, years * 12);
}
}