创建一个长度为6的 int 型数组,要求取值为 1- 30,同时元素值各不相同
public static void main(String[] args) {
int[] arr = new int[6];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 30) + 1;
boolean flag = false;
while (true){
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]){
flag = true;
break;
}
}
if (flag){
arr[i] = (int) (Math.random() * 30) + 1;
continue;
}
break;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
int[] arr1 = new int[6];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = (int) (Math.random() * 30) + 1;
for (int j = 0; j < i; j++) {
if (arr1[i] == arr1[j]){
i--;
break;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr1[i]);
}
}