📜  Java程序在字符串上实现switch语句

📅  最后修改于: 2020-09-26 18:38:01             🧑  作者: Mango

在此示例中,我们将学习在Java中对字符串实现switch语句。

示例:在字符串上实现switch语句
// Java Program to implement String on switch statements in Java

class Main {
  public static void main(String[] args) {

    // create a string
    String language = "Java";

    switch(language) {

      case "Java":
        System.out.println(language + " is famous for enterprise applications.");
        break;

      case "JavaScript":
        System.out.println(language + " is famous for frontend and backend.");
        break;

      case "Python":
        System.out.println(language + " is famous for ML and AI.");
        break;

      default:
        System.out.println(language + " not found on record.");
        break;
    }
  }
}

输出

Java is famous for enterprise applications.

在上面的示例中,我们在字符串上实现了switch语句。 Java 7中引入了此功能。