【发布时间】:2014-03-29 19:37:05
【问题描述】:
我的程序有一行,它根据使用Math.pow 方法的公式计算复利。
当变量loanRate在下面的程序的原始版本中被声明为Integer时,公式根本不起作用,只会返回一个0;
我将loanRate 更改为Double,如下所示,由于某种原因,现在程序正在运行。
很抱歉,如果这是一个非常简单的问题,我只是不知道为什么 Math.pow 方法不适用于我的 Int 以及使用 Math.pow 背后是否有一个我缺少的一般原则?
提前感谢您的帮助。
// Variables & Constants
int principle, i;
double simpleInt, compoundInt, difference, loanRate;
// Prompts user to enter principle and rate
System.out.print("Enter Principle: "); // keep print line open
principle = console.nextInt();
System.out.println();
System.out.print("Enter Rate: ");
loanRate = console.nextDouble();
// Header
// Caculates and Outputs Simple, Compound and Difference for the loan
// in 5 year intervals from 5 to 30
for (i = 5; i <= 30; i = i + 5 )
{
simpleInt = principle * loanRate/100 * i;
compoundInt = principle * ((Math.pow((1 + loanRate/100),i))-1);
difference = compoundInt - simpleInt;
System.out.printf("\n %7d %7.2f %7.2f %7.2f", i, simpleInt, compoundInt, difference);
}
【问题讨论】:
-
只是因为你在执行整数除法。
标签: java int double type-conversion