4
我想在java中运行此代码并获得以下错误,要求int发现双精度。可能会丢失精度
public class UseMath{
public static void main(String[]args){
int x = Math.pow (2,4);
System.out.println ("2 to the power of 4 is" + x);
}
}
请帮助....
我想在java中运行此代码并获得以下错误,要求int发现双精度。可能会丢失精度
public class UseMath{
public static void main(String[]args){
int x = Math.pow (2,4);
System.out.println ("2 to the power of 4 is" + x);
}
}
请帮助....
如果你看一看的documentation,它说,这Math.pow()
需要两个doubles
,并返回一个double
。当你将这个函数传入int时,这意味着没有伤害,因为将int
转换为double
意味着没有损失。但是,如果将值分配给int
,则表示它可能会失去精度。
只要做到这一点:
int x = (int)Math.pow (2,4);
或
double x = Math.pow (2,4);
谢谢我现在得到了输出。 – Sandra 2013-02-27 21:34:59
你在哪里得到的错误?你确定这不是相反的方式吗? [Oracle说](http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html)'Math.pow()'需要双打,你给它'ints' – MikeTheLiar 2013-02-27 21:21:41
声明'x'为'double x = ...' – SRy 2013-02-27 21:25:45