深入理解 Java 中的 Switch 語句示例詳解
深入理解 Java 中的 Switch 語句
在 Java 編程中,switch
語句是一種強大的控制結(jié)構(gòu),能夠根據(jù)表達式的值選擇執(zhí)行不同的代碼塊。本文將詳細介紹 switch
的基本語法、使用案例、注意事項以及與 if
語句的選擇。
基本語法
switch
語句的基本語法如下:
switch (表達式) { case 常量1: // 執(zhí)行代碼塊1 break; case 常量2: // 執(zhí)行代碼塊2 break; ... default: // 執(zhí)行默認代碼塊 }
關(guān)鍵點
- 關(guān)鍵字:使用
switch
來聲明分支結(jié)構(gòu)。 - 表達式:該表達式的值用于選擇相應的
case
。 - case 常量:當表達式的值等于某個常量時,將執(zhí)行對應的代碼塊。
- break:用于退出
switch
,防止繼續(xù)執(zhí)行下一個case
。 - default:可選的,當沒有任何
case
匹配時執(zhí)行。
程序流程圖
示例代碼
以下是一個簡單的示例,展示如何根據(jù)輸入的字符輸出對應的星期幾:
import java.util.Scanner; public class SwitchExample { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入一個字符(a-g):"); char c1 = in.next().charAt(0); switch (c1) { case 'a': System.out.println("星期一"); break; case 'b': System.out.println("星期二"); break; case 'c': System.out.println("星期三"); break; case 'd': System.out.println("星期四"); break; case 'e': System.out.println("星期五"); break; case 'f': System.out.println("星期六"); break; case 'g': System.out.println("星期日"); break; default: System.out.println("輸入的內(nèi)容不正確"); } System.out.println("退出Switch,繼續(xù)執(zhí)行程序"); } }
注意事項
在使用 switch
語句時,需注意以下幾點:
- 數(shù)據(jù)類型匹配:表達式的數(shù)據(jù)類型應與
case
后的常量類型一致,或能自動轉(zhuǎn)換。 - 支持的數(shù)據(jù)類型:
switch
的表達式可以是(byte, short, int, char, enum, String)
。 - case 子句:
case
中的值必須是常量,不能是變量。 - default 子句:可選的,未匹配時執(zhí)行。如果不寫,可能不會有輸出。
- break 語句:用于結(jié)束當前
case
的執(zhí)行,若不寫,則會順序執(zhí)行下一個case
直到遇到break
。
使用細節(jié)
public class SwitchDetail { public static void main(String[] args) { char c = 'a'; System.out.println(c); // 輸出 a System.out.println((int) c); // 輸出 97 switch (c) { case 'a': System.out.println("ok1"); break; case 'b': System.out.println("ok1"); break; case 20: // char 類型可以轉(zhuǎn)換為整數(shù) System.out.println("ok1"); break; default: System.out.println("輸入錯誤。"); } //使用String類型示例,過程如下 // char d = "a" // switch(c){ // case "a": // System.out.println("ok1"); // break; // case "b": // System.out.println("ok1"); // break; // case 20: //這里 會報錯,這里的類型和String類型不能轉(zhuǎn)換 // System.out.println("ok1"); // break; // default: // System.out.println("輸入錯誤。"); // } } }
練習
- 使用 switch 把小寫類型的 char 型轉(zhuǎn)為大寫(鍵盤輸入)。只轉(zhuǎn)換 a, b, c, d, e. 其它的輸出 “other”。
- 對學生成績大于 60 分的,輸出"合格"。低于 60 分的,輸出"不合格"。(注:輸入的成績不能大于 100), 提示 成績/60
- 根據(jù)用于指定月份,打印該月份所屬的季節(jié)。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季 [課堂練習, 提示 使用穿透 ]
練習 1:小寫字母轉(zhuǎn)大寫
import java.util.Scanner; public class SwitchExercise { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入一個小寫英文字母(a-e):"); char c1 = in.next().charAt(0); switch (c1) { case 'a': case 'b': case 'c': case 'd': case 'e': System.out.println(c1 + "的大寫是" + (char) (c1 - 32)); break; default: System.out.println("other"); } } }
練習 2:成績判斷
// 這里使用編程思路,將成績的范圍轉(zhuǎn)換成一個整數(shù)類型來進行判斷, // [0-60]使用除法的思想把轉(zhuǎn)換成一個整數(shù) // [60- 100] 同理使用相同的思路 import java.util.Scanner; public class SwitchExercise02 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入你的分數(shù):"); int grade = in.nextInt(); if (grade >= 0 && grade <= 100) { switch (grade / 60) { case 0: System.out.println("不合格"); break; case 1: System.out.println("合格"); break; default: System.out.println("輸入有誤,請重新輸入!??!"); } } } }
練習 3:季節(jié)判斷
// 穿透的意思就是使用一個條件能夠使所有的條件都能夠符合使用 import java.util.Scanner; public class SwitchExercise03 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入一個月份:"); int month = in.nextInt(); switch (month) { case 3: case 4: case 5: System.out.println("春季"); break; case 6: case 7: case 8: System.out.println("夏季"); break; case 9: case 10: case 11: System.out.println("秋季"); break; case 12: case 1: case 2: System.out.println("冬季"); break; default: System.out.println("輸入有誤,請重新輸入?。?!"); } } }
switch 與 if 的選擇
- 使用場景:當判斷的具體數(shù)值不多且符合
byte, short, int, char, enum, String
類型時,推薦使用switch
語句。 - 適用范圍:對于區(qū)間判斷和布爾類型的判斷,使用
if
語句更為靈活。
通過上述內(nèi)容,相信你對 Java 中的 switch
語句有了更深入的理解。希望這篇文章能幫助你在編程中更加得心應手!
到此這篇關(guān)于深入理解 Java 中的 Switch 語句的文章就介紹到這了,更多相關(guān)Java Switch 語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring容器初始化擴展點之ApplicationContextInitializer詳解
ApplicationContextInitializer是Spring框架提供的一個接口,用于在Spring應用上下文刷新之前對其進行自定義初始化,本文介紹Spring容器初始化擴展點之ApplicationContextInitializer,感興趣的朋友一起看看吧2025-02-02SpringBoot+MyBatisPlus+Vue 前后端分離項目快速搭建過程(后端)
這篇文章主要介紹了SpringBoot+MyBatisPlus+Vue 前后端分離項目快速搭建過程(后端),快速生成后端代碼、封裝結(jié)果集、增刪改查、模糊查找,畢設基礎(chǔ)框架,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05解決IDEA中下載free maven plugin插件無效的問題
這篇文章主要介紹了解決IDEA中下載free maven plugin插件無效的問題,本文通過圖文并茂的形式給大家分享解決方案,供大家參考,需要的朋友可以參考下2020-11-11