JAVA 高手来看下 菜鸟求教

来源:百度知道 编辑:UC知道 时间:2024/06/29 00:04:52
编写一个JAVA Application程序
功能是输出10个0~9之间的偶数
代码是
public class Uuu
{public static void main(String[] args)
{int n=1,x;
while(n<=10);
{x=Math.random()*10;
if (x%2==0)
{System.out.println(x);
n++;}
}
}
}
问题是当编译的时候会出现
Uuu.java:5:可能损失精度
找到:double
需要:int
{x=Math.random()*10;

1错误

请问各位高手为什么会这样 这个是书上的例子 应该不会错啊???

(int)Math.random()*10 这样肯定有错
应该是(int)(Math.random()*10 )
上面会将Math.random()强制转化成int型只能是0了

randon()返回的是double型的。可以进行强制类型准换
double temp=Math.random()*10;
x=(int)temp;
加以上语句

hebsong 说的有道理,不是楼上的那几位大虾不会,我估计是太粗心了。程序是调出来的,不是写出来的。所以没必要记得那么死板。

兄弟,学习的时候不要怕花钱
买本好点的书贵不了多少钱的

你的程序问题很大,我试了一下,编是编译出来,就是不能运行。
我给你写一个:
class Uuu{
public static void main(String args[]){
int n=1;
while(n<=10){
n+=1;
if(n%2==0){
System.out.println(n);
}
}
}
}
上面说你有是精度是要将int变成double,不要迷信与课本上面会有很多的错误,我的脚踩是很正版的上面也有很多的错误!

你的程序不止一个错误,我想下面这个程序是你想要的,运行下一就知道了,再看看和你的有什么区别
public class Uuu
{public static void main(String[] args)
{int n=1,x;
while(n<=10)
{x=(int)(10*Math.random());
if (x%2==0)
{System.out.println(x);}
else{
continue;}
n++;
}
}
}