跳到內容

Java 程式設計/關鍵字/case

來自 Wikibooks,一個開放世界的開放書籍

case 是一個 Java 關鍵字。

這是 switch 語句的一部分,用於查詢傳遞給 switch 語句的值是否與後面跟有 case 的值匹配。

例如

Computer code
int i = 3;
switch(i) {
case 1:
   System.out.println("The number is 1.");
   break;
case 2:
   System.out.println("The number is 2.");
   break;
case 3:
   System.out.println("The number is 3."); // this line will print
   break;
case 4:
   System.out.println("The number is 4.");
   break;
case 5:
   System.out.println("The number is 5.");
   break;
default:
  System.out.println("The number is not 1, 2, 3, 4, or 5.");
}
華夏公益教科書