键盘随机输入一组数据并存到数组中,并对数组中的值进行随机排序
public static void main(String[] args) {
int[] arr1 = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i <arr1.length ; i++) {
System.out.println("请输入第"+(i+1)+"个数字");
arr1[i] = sc.nextInt();
}
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i]+"\t");
}
Random r = new Random();
for (int i = 0; i < arr1.length; i++) {
int index = r.nextInt(arr1.length);
int temp = arr1[index];
arr1[index] = arr1[i];
arr1[i] = temp;
}
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i]+"\t");
}
}
|