【发布时间】:2015-05-27 01:44:46
【问题描述】:
我正在尝试计算 Java 的未来投资金额。 我的程序运行,但它没有给我正确的答案。
如果
- 投资金额为1000.56,
- 利率为4.25,
- 年数为 1,
- 答案应该是 1043.92 美元。
我们必须使用的公式是futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) ^ numberOfYears * 12
下面是我的课
// Import Java Scanner
import java.util.Scanner;
public class Ex_2_21 {
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
//Prompt the user to enter investment amount, annual interest rate, and number of years
System.out.println("Enter investment amount:");
System.out.println("Enter annual interest rate:");
System.out.println("Enter number of years:");
float investmentamount = input.nextFloat();
float interestrate = input.nextFloat();
float numberofyears = input.nextFloat();
float years = numberofyears * 12;
//Formula to calculate the accumulated value
float futureInvestmentValue = (float) (investmentamount * Math.pow((years), 1 + interestrate));
//Print result
System.out.println("The accumulated value is " + futureInvestmentValue);
}
}
【问题讨论】:
标签: java floating-point finance