import java.util.Scanner;
class ArrayTest4{
public static void main(String[] args){
int[] arr = new int[5];
Scanner sc = new Scanner(System.in);
System.out.println("请依次输入五个整数:");
for(int x=0; x<arr.length; x++){
arr[x] = sc.nextInt();
}
printArray(arr);
printArray(selectSort(arr));
}
public static int[] selectSort(int[] arr){
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[x]>arr[y]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
return arr;
}
public static void printArray(int[] arr){
System.out.print("[");
for(int x=0; x<arr.length; x++){
if(x!=arr.length-1){
System.out.print(arr[x]+", ");
}else{
System.out.print(arr[x]+"]");
}
}
System.out.println(";");
}
}
|
|