Java 中 < ins>Switch Case 中的字串


Java 7 的推出增強了 < ins>Switch Case,即它也支援字串。

首先,設定一個字串-​​

String department = "AKD05";

現在,在 < ins>SWITCH 中使用相同的字串,如下所示 -

switch(department)

示例

現在,使用用例檢查每個字串,就像我們在使用 < ins>SWITCH CASE 時通常所做的一樣。以下是實現 < ins>SWITCH CASE 中字串的示例 -

public class Demo {
   public static void main(String[] args) {
      String department = "AKD05";
      switch(department) {
         case "AKD01":
            System.out.println("Finance");
            break;
         case "AKD02":
            System.out.println("Sales");
            break;
         case "AKD03":
            System.out.println("Production");
            break;
         case "AKD04":
            System.out.println("Marketing");
            break;
         case "AKD05":
            System.out.println("Operations");
            break;
         default:
            System.out.println("None!");
      }
   }
}

輸出

Operations

更新於: 25-Sep-2019

226 瀏覽

為你的職業生涯助力

完成課程獲得認證

開始吧
廣告
© . All rights reserved.