返回列表 發帖

[隨堂測驗] 對陣列排序 (二)

本帖最後由 李泳霖 於 2023-2-7 14:51 編輯

運用 Scanner 類別下的 nextInt() 方法 與 Arrays 類別下的 sort() 方法, 完成如下畫面之程式:

  1. import java.util.*;
  2. public class Ch45
  3. {
  4.     public static void main(String args[])
  5.     {
  6.          Scanner s=new Scanner(System.in);
  7.          int n[]=new int[10];
  8.          System.out.println("請任意輸入10個整數");
  9.          for(int i=0; i<10; i++)
  10.          {
  11.              System.out.print("第"+(i+1)+"個數: ");
  12.              n[i]=s.nextInt();
  13.          }
  14.          Arrays.sort(n);
  15.          System.out.print("剛輸入的10個數由小而大依序為: ");
  16.          for(int i=0; i<10; i++)
  17.              System.out.print(n[i]+" ");
  18.          System.out.println();
  19.     }
  20. }
複製代碼
istak.teach2@gmail.com

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

返回列表