- 提取出来的场景模拟:对于1–16,调用一次方法,就让他产生4个数字,调用4次完毕,产生4组不同的数字!
注意:nextInt(101)是产生0-100之间的任意整数,不包括101.
*
完整代码:public class World {
static int a[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
public static void random(){
System.out.println(“——————–”);
int count=0;
Random random = new Random();
while (true) {
int index = random.nextInt(16);//产生下标为0-15之间!
if (a[index] != 0) {
count++;
System.out.print(a[index]+” “);
a[index] = 0;
if (count == 4)
break;
}
}
System.out.println();}
public static void main(String [] args){
for(int i=1;i<=4;i++){
random();
}
}
}
测试输出:
3 11 2 9
16 1 7 10
12 8 13 15
4 6 5 14