程序实现:键盘随意输入一串数字,按从小到大。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//动态创建数组
int[] arr = new int[sc.nextInt()];
for(int i = 0 ; i< arr.length ; i++){
arr[i] = sc.nextInt();
}
for(int i = 1 ; i< arr.length ; i++){
for(int j = 0;j<arr.length-i;j++ ){
if(arr[j]>arr[j+1]){
int sum = arr[j];
arr[j] = arr[j+1];
arr[j+1] = sum;
}
}
}
System.out.print(Arrays.toString(arr));
}
}