美文网首页
Java的switch case

Java的switch case

作者: weiliping | 来源:发表于2018-06-10 11:30 被阅读0次

    Java中的switch case等价于if else if语句。它用于根据某些条件执行语句。

    switch(expression) {
        case value_1 :
           // Statements
           break; // optional
           
        case value_2 :
           // Statements
           break; // optional
           
        // If expression does not match with any of above conditions, 
        // default will be executed 
        default : // Optional
           // Statements
    }
    

    我们通过一个示例来理解它。我们将通过一个integer数字来打印工作日:

    package org.ifelse;
    
    public class SwitchDemo {
        public static void main(String[] args) {
            int dayOfWeek = 5;
            switch (dayOfWeek) {
            case 0:
                System.out.println("Sunday");
                break;
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thrusday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
    
            // Default is executed when expression does not match with any of above
            // conditions.
            default: // Optional
                System.out.println("Invalid day of week");
            }
        }
    }
    

    程序运行后结果显示如下:

    Friday

    假如将上面示例中的break移除,看看会发生什么:

    package org.ifelse;
    
    public class SwitchDemo {
        public static void main(String[] args) {
            int dayOfWeek = 5;
            switch (dayOfWeek) {
            case 0:
                System.out.println("Sunday");
            case 1:
                System.out.println("Monday");
            case 2:
                System.out.println("Tuesday");
            case 3:
                System.out.println("Wednesday");
            case 4:
                System.out.println("Thrusday");
            case 5:
                System.out.println("Friday");
            case 6:
                System.out.println("Saturday");
    
            // Default is executed when expression does not match with any of above
            // conditions.
            default: // Optional
                System.out.println("Invalid day of week");
            }
        }
    }
    

    程序运行结果显示如下:

    Friday
    Saturday
    Invalid day of week

    正如你在上面示例中看到的那样,如果你不使用break语句,它会在条件满足时执行条件后的所有语句。

    switch case的string示例:
    在Java 7之后的switch表达式中使用String。

    package org.ifelse;
    
    public class SwitchDemo {
        public static void main(String[] args) {
            String dayOfWeek = "5";
            switch (dayOfWeek) {
            case "0":
                System.out.println("Sunday");
                break;
            case "1":
                System.out.println("Monday");
                break;
            case "2":
                System.out.println("Tuesday");
                break;
            case "3":
                System.out.println("Wednesday");
                break;
            case "4":
                System.out.println("Thrusday");
                break;
            case "5":
                System.out.println("Friday");
                break;
            case "6":
                System.out.println("Saturday");
                break;
    
            // Default is executed when expression does not match with any of above
            // conditions.
            default: // Optional
                System.out.println("Invalid day of week");
            }
        }
    }
    

    程序运行结果显示如下:

    Friday

    switch case在内部使用equals方法比较,所以case语句在这里区分大小写。

    在switch case和if else if之间进行选择取决于可读性和各种因素。你可以根据自己的需要进行选择。

    相关文章

      网友评论

          本文标题:Java的switch case

          本文链接:https://www.haomeiwen.com/subject/lfjfeftx.html