返回列表 發帖

對陣列排序 (二)

本帖最後由 tonyh 於 2012-12-1 17:08 編輯

運用 Scanner 物件, 取代原本的 Console 物件, 來抓取使用者輸入的資料,
改寫上一個我們寫的程式.
  1. import java.util.*;
  2. public class ch46
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         int a[]=new int[10];   //宣告陣列並產生陣列實體
  7.         Scanner input=new Scanner(System.in);
  8.         System.out.println("請任意輸入10個整數");
  9.         for(int i=0; i<10; i++)
  10.         {
  11.             System.out.print("第"+(i+1)+"個數: ");
  12.             a[i]=input.nextInt();
  13.         }
  14.         Arrays.sort(a);    //對陣列排序
  15.         System.out.print("您剛剛輸入的10個數由小而大依序為: ");
  16.         for(int i=0; i<10; i++)
  17.         {
  18.             System.out.print(a[i]+" ");
  19.         }
  20.     }
  21. }
複製代碼

返回列表