首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

突然很奇怪(int)Math.random()*10只等于0,该怎么处理

2013-01-25 
突然很奇怪(int)Math.random()*10只等于0int sign//全局变量sign (int)Math.random()*10//方法中调用,

突然很奇怪(int)Math.random()*10只等于0


int sign;    //全局变量


sign = (int)Math.random()*10;  //方法中调用,sign在whil(true)中一直等于0;




//代码

public void getLotteryNumber() throws Exception{
//System.out.println(sign);
int sign_0 = 0;
int time = 0;
while(true){
sign = (int)Math.random()*10;
//sign = 9;
System.out.println((this.getClass().getResource("") +"pictrue/" + sign + ".jpg").substring(5, (this.getClass().getResource("") +"pictrue/" + sign + ".jpg").length()));
labelNumber [sign_0].setIcon(new ImageIcon((this.getClass().getResource("") +"pictrue/" + sign +".jpg").substring(5, (this.getClass().getResource("") +"pictrue/" +sign + ".jpg").length())));
labelNumber [sign_0].updateUI();
sign_0 ++;
time ++;
if(sign_0 == 5){
sign_0 = 0;
}
if(time == 100){
break;
}
Thread.sleep(100);
}
}
[解决办法]
因为先执行了(int)Math.random()后又执行的*10操作
[解决办法]
 (int)Math.random()*10;  //方法中调用,sign在whil(true)中一直等于0;
他其实是怎么算的 ((int)Math.random())*10; 
你应该这么写(int) (Math.random() *10); 
[解决办法]
汗一个...Math.random的返回内容是从0-1的小数
你直接给他转成int,当然是0了...

另:获取随机数建议用Random类

Random ran = new Random();
System.out.println(ran.nextInt(10));

热点排行