import?java.util.Random;
public?class?random?{?
public?static?void?main(String[]?args)?{? int[]?numArray?=?new?int[30]; //生成壹個存放30個數字的數組 for(int?i=0;i<numArray.length;i++){numArray[i]=i+1;
} Random?random?=?new?Random(); int?randomNum?=?numArray.length-1; int[]?array=new?int[7]; for(int?i=0;i<array.length;i++){int?index?=?random.nextInt(randomNum--);
array[i]=?numArray[index];
int?temp=numArray[index];
numArray[index]=numArray[randomNum];
numArray[randomNum]=temp;
} Arrays.sort(array); for(int?i=0;i<array.length;i++){System.out.print(array[i]+"?");
} }說下大致思路,就是先弄壹個1-30的數組,然後隨機去數組裏面取數,取完壹次以後把本次取的數據往數組尾部放,並把尾部沒選過的數字放到中間來,然後縮小檢索數組的範圍(這樣就不會選到選過的數字了)