返回列表 發帖

switch 判斷式

本帖最後由 tonyh 於 2014-11-1 17:42 編輯

將成績分級程式, 以 switch 語法改寫.
90分~100分  優
  80分~89分  甲
  70分~79分  乙
  60分~69分  丙
    0分~59分  丁
  1. import java.io.Console;
  2. public class ch12
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.          int score, level;
  7.          Console c=System.console();
  8.          System.out.print("請輸入你的成績: ");
  9.          score=Integer.parseInt(c.readLine());
  10.          level=score/10;
  11.          switch(level)
  12.          {
  13.               case 10:
  14.               case 9:
  15.                   System.out.println("優");
  16.                   break;
  17.               case 8:
  18.                   System.out.println("甲");
  19.                   break;
  20.               case 7:
  21.                   System.out.println("乙");
  22.                   break;
  23.               case 6:
  24.                   System.out.println("丙");
  25.                   break;
  26.               case 5:
  27.               case 4:
  28.               case 3:
  29.               case 2:
  30.               case 1:
  31.               case 0:
  32.                   System.out.println("丁");
  33.                   break;
  34.               default:
  35.                   System.out.println("輸入錯誤!");
  36.          }
  37.     }
  38. }
複製代碼

返回列表