java模拟考试试卷的乱序处理,安排一个一维数组存放1-20之间不重复的数值,产生一个新的数组存放原来数组元素乱序处理后的数据。分别打印出新旧数组。
时间: 2024-03-05 19:55:06 浏览: 69
好的,下面是 Java 代码实现:
```
import java.util.*;
public class ExamPaper {
public static void main(String[] args) {
int[] oldArr = new int[20];
for (int i = 0; i < 20; i++) {
oldArr[i] = i + 1;
}
int[] newArr = Arrays.copyOf(oldArr, oldArr.length);
shuffleArray(newArr);
System.out.println("原数组为:" + Arrays.toString(oldArr));
System.out.println("新数组为:" + Arrays.toString(newArr));
}
public static void shuffleArray(int[] arr) {
Random rand = new Random();
for (int i = arr.length - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
```
运行结果:
```
原数组为:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
新数组为:[18, 2, 17, 3, 6, 11, 7, 12, 8, 10, 20, 1, 5, 19, 13, 15, 9, 4, 14, 16]
```
阅读全文