Java 当变量的类型为int时,Math.pow不起作用
我的程序有一行,它根据使用Java 当变量的类型为int时,Math.pow不起作用,java,int,double,type-conversion,Java,Int,Double,Type Conversion,我的程序有一行,它根据使用Math.pow方法的公式计算复利 当变量loanRate在下面程序的原始版本中声明为整数时,公式根本不起作用,只返回0 我将loanRate更改为Double,如下所示,出于某种原因,程序正在运行 很抱歉,如果这是一个非常简单的问题,我只是不知道为什么Math.pow方法不能与我的Int一起工作,以及使用Math.pow是否有我所缺少的一般原则 提前感谢您的帮助 // Variables & Constants int principle, i; double
Math.pow
方法的公式计算复利
当变量loanRate
在下面程序的原始版本中声明为整数
时,公式根本不起作用,只返回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);
}
//变量和常量
int原则,i;
双重简单、复合、差异、贷款;
//提示用户输入原则和费率
系统输出打印(“输入原则:”;//保持打印线路畅通
principle=console.nextInt();
System.out.println();
系统输出打印(“输入速率:”);
loanRate=console.nextDouble();
//标题
//计算并输出贷款的简单、复合和差异
//每隔5年从5到30
对于(i=5;icompoundInt=principle*((Math.pow((1+loanRate/100),i))-1);
loanRate/100
这里是关键,因为loadRate
是<100,这样除法的结果将是0。将此更改为loanRate/100.0
,这将解决问题。这只是因为您执行了整数除法。非常感谢-现在这非常合理!)