- 提取出來的場景模擬:對於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