package tag; import java.util.Scanner; /* * 从键盘输入n个数,升序输出 * */ public class Test1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("请输入元素个数"); int number = 0; try { number = in.nextInt(); } catch (Exception e) { System.out.print("您的输入有误"); } int[] arr = new int[number]; for (int i = 0; i < arr.length; i++) { // for循环进行数组元素从键盘输入 System.out.print("请输入第" + (i + 1) + "个数"); try { arr[i] = in.nextInt(); // 调用Scanner类中的方法.nextInt(),获取输入值 } catch (Exception e) { System.out.print("您的输入有误"); return; } } function(arr); } public static void function(int[] array) { int j, k, t; t = 0; for (j = 0; j < array.length - 1; j++) { for (k = 0; k < array.length - 1 - j; k++) { // 控制每轮比较的次数 if (array[k] > array[k + 1]) { t = array[k]; array[k] = array[k + 1]; array[k + 1] = t; } } } for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); } }