集册 Java实例教程 在switch语句中使用字符串

在switch语句中使用字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

552
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在switch语句中使用字符串

public class Main {

  public static void main(String[] args) {

    operate("on");/*来 自 时代Java*/

    operate("off");

    operate("ON");

    operate("Nothing");

    operate("OFF");

    operate("1");

    operate(null);

  }


  public static void operate(String status) {

    // Check for null 

    if (status == null) {

      System.out.println("status cannot be null.");
      /*
      N o w  J a v a  .   c o m 提供
      */

      return;

    }


    // Convert to lowercase 

    status = status.toLowerCase();


    switch (status) {

      case "on":

        System.out.println("Turn on");

        break;

      case "off":

        System.out.println(
展开阅读全文