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操作汗一个...Math.random的返回内容是从0-1的小数
你直接给他转成int,当然是0了...
另:获取随机数建议用Random类
(int)Math.random()*10; //方法中调用,sign在whil(true)中一直等于0;
Random ran = new Random();
System.out.println(ran.nextInt(10));
他其实是怎么算的 ((int)Math.random())*10;
你应该这么写(int) (Math.random() *10);