使用Math.random随机生成100个唯一数字

时间:2018-12-12 03:03:23

标签: java

我想打印出这100个数字的数组。但是,我的程序在生成一定数量的数字后停止。这是我的代码。

class A4
    public static void main(String[] args)
    {
        int[] array1 = new int[101];
        int num, i = 0;
        boolean repeat = false;
        while(i < 100)
        {   
            num = (int) (Math.random() * 100) + 1;
            array1[(i)] = num;
            for (int e = 0; e < i; e++)
            {
                if (num == array1[e])
                    repeat = true;
            }

            if (repeat == true)
            {
                continue;
            }

            array1[i] = num;
            System.out.println(array1[i]);
            i++;
        }
    }
}

0 个答案:

没有答案
相关问题