Java:编写一个程序,在其中建立一个有10个整数的数组,运行后从键盘输入10个数,然后排序(升序)后输出。
/*编写一个程序,在其中建立一个有10个整数的数组,运行后从键盘输入10个数,然后排序(升序)后输出。*/
package com.company;
import java.util.Scanner;
public class Sort {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
for(int i=0; i<10; i++)
arr[i] = in.nextInt(); //输入十个数字存放在数组中
bubbleSort(arr);
for(int j=0; j<10; j++)
{
System.out.println(arr[j]+" ");
}
}
public static void bubbleSort(int[] arr){ //冒泡排序
int temp;
for(int i=0; i<arr.length-1; i++)
{
for(int j=0; j<arr.length-1-i; j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}