定义一个长度为5的int型数组arr,数组元素为1-5(范围包含1和5)之间的任意随机数,且保证5个数不重复

    public static void main(String[] args) {

        int[] arr = new int[5];
        Random ran = new Random();

        for (int i = 0; i < arr.length; i++) {

            int random = ran.nextInt(5) + 1;
            arr[i] = random;
            for (int j = 0; j < i; j++) {
                if (arr[i] != arr [j]) {
                }else {
                  i--;
                }
            }

        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        
    }

上一篇:题解 Emotional Flutter


下一篇:Random类