包含 Math.pow() 的公式产生意外结果



我正在尝试编写一个程序,该程序需要三个输入并将它们通过以下等式:

futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate)^(numberOfYears*12)

我正在使用的正确示例结果说,对于investment amount = 1000.56annual interest rate = 3.25(因此monthlyInterestRate = annual / 12(和numberOfYears = 1,我应该得到futureInvestmentValue = 1032.98

但是,我的程序陈述了"$38045.96184617848"的最终结果(注意:我不需要四舍五入(。

为什么会这样?

法典:

import java.util.Scanner;
public class num2_21 {
public static void main(String[] args) {
    System.out.println("This program was designed to calculate the future investment value of an investment. nn"
            + "When prompted, please enter the initial investment amount, interest APR (%), and length of investment (yrs). n"
            + "Investment amount: ");
    Scanner in1 = new Scanner(System.in);
    double investAmount = in1.nextDouble();
    System.out.println("n" + "Annual interest rate in percentage: ");
    Scanner in2 = new Scanner(System.in);
    double APR = in2.nextDouble();
    double monthlyInterest = APR / 12;
    System.out.println("n" + "Length of investment: ");
    Scanner in3 = new Scanner(System.in);
    double investLength = in3.nextDouble();
    double futureInvestmentValue = investAmount * Math.pow((1 + monthlyInterest),(investLength*12));
    System.out.println("n" + "Accumulated value: $" + futureInvestmentValue);

}
}

安慰:

This program was designed to calculate the future investment value of an investment. 
When prompted, please enter the initial investment amount, interest APR (%), and length of investment (yrs). 
Investment amount: 
1000.56
Annual interest rate in percentage: 
4.25
Length of investment: 
1
Accumulated value: $38045.96184617848

编辑:我忘了将 APR 除以 100。我没有意识到我需要将其转换为小数。对疏忽表示歉意。我很欣赏多一双眼睛。谢谢你的时间。

嗯,这可能是因为 425.0% 的年利率还不错,也可能是因为您忘记除以 100:

    double APR = in2.nextDouble() / 100.0;

最新更新