因为点过的不能再被点到,所以将点过的数储存在数组result中,引入布尔类型变量exist
import java.util.Random;
public class SuiJi {
public static void main(String[] args) {
int result[] = new int[3];
int count = 0;
Random ran = new Random();
int i;
while(count<3) {
int s = ran.nextInt(96);
boolean exist = true;
for(i=0;i<=count;i++) {
if(s==result[i]) {
exist =true;
break;
}
else {
exist=false;
}
}
if (exist==true) {
continue;
}
else {
result[count]=s;
count++;
}
}
for(i=0;i<result.length;i++) {
System.out.println("恭喜第"+result[i]+"位员工");}
}
}
1.循环的大括号位置要注意
2。全局变量和局部变量要注意
最后
以上就是孤独曲奇最近收集整理的关于Java用Random写简单的抽奖的全部内容,更多相关Java用Random写简单内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复