java不重複亂數1~10,並將亂數出的數由小到大排序
本帖最後由 零售商 於 2012-3-12 19:08 編輯
- import java.util.Arrays;
- public class Test01_0224 {
- public static void main(String[] args) {
- int [] intA = new int[11];
- int temp,temp2,gn = 5;
- for (int i = 1 ; i < intA.length ; i++)
- intA[i] = i;
- for(int i = 1 ; i <= gn ; i++){
- temp = (int)(Math.random() * (intA.length-i) + i);
- temp2 = intA[i];
- intA[i] = intA[temp];
- intA[temp] = temp2;
- }
- for(int i = gn + 1; i < intA.length ; i++)
- intA[i] = intA.length;
- Arrays.sort(intA);
- for(int i = 1 ; i <= gn ; i++)
- System.out.print(intA[i] + ",");
- }
- }
複製代碼 |