面試手寫代碼的題目

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

發佈了72 篇原創文章 · 獲贊 15 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章