- package bn.tw;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
- Scanner scanner = new Scanner(System.in);
- int input = scanner.nextInt();
- int n[] = new int[input];
- for (int i = 0; i < n.length; i++)
- n[i] = scanner.nextInt();
- System.out.println("排序前:");
- for (int i = 0; i < n.length; i++)
- System.out.print(n[i] + " ");
- System.out.println();
- Arrays.sort(n);
- System.out.println("排序後:");
- try {
- for (int i = 0; i <= n.length; i++)
- System.out.print(n[i] + " ");
- } catch (Exception ex) {
- System.out.println();
- System.out.println("陣列索引超出範圍");
- }
- }
- }
複製代碼 |