{"id":705,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java Switch\u8bed\u53e5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/705.html","title":{"rendered":"Java Switch\u8bed\u53e5"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java Switch\u8bed\u53e5<\/h1>\n<\/p><\/div>\n
\n Java
\n switch\u8bed\u53e5<\/em>\u4ece\u591a\u4e2a\u6761\u4ef6\u4e2d\u6267\u884c\u4e00\u6761\u8bed\u53e5\u3002\u5c31\u50cf
\n if-else-if \u9636\u68af\u8bed\u53e5\u3002 switch\u8bed\u53e5\u9002\u7528\u4e8e\u5b57\u8282\uff0c\u77ed\u578b\uff0c\u6574\u578b\uff0c\u957f\u578b\uff0c\u679a\u4e3e\u7c7b\u578b\uff0c\u5b57\u7b26\u4e32\u548c\u67d0\u4e9b\u5305\u88c5\u7c7b\u578b\uff0c\u5982byte, short, int, long\u3002\u4eceJava 7\u5f00\u59cb\uff0c\u60a8\u53ef\u4ee5\u5728switch\u8bed\u53e5\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u3002\n <\/div>\n
\n \u6362\u53e5\u8bdd\u8bf4\uff0cswitch\u8bed\u53e5\u9488\u5bf9\u591a\u4e2a\u503c\u6d4b\u8bd5\u53d8\u91cf\u7684\u76f8\u7b49\u6027\u3002\n <\/div>\n

Java Switch\u8981\u8bb0\u4f4f\u7684\u8981\u70b9<\/h3>\n

Switch\u8868\u8fbe\u5f0f\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216N\u4e2a\u4e2a\u6848\u503c<\/em>\u3002<\/span>
\n case\u503c\u53ea\u80fd\u662fswitch\u8868\u8fbe\u5f0f\u7c7b\u578b\u3002\u5927\u5c0f\u5199\u503c\u5fc5\u987b\u4e3a\u6587\u5b57\u6216\u5e38\u91cf<\/em>\u3002\u5b83\u4e0d\u5141\u8bb8\u53d8\u91cf\u3002<\/span>
\n \u6848\u4f8b\u503c\u5fc5\u987b\u4e3a\u552f\u4e00<\/em>\u3002\u5982\u679c\u503c\u91cd\u590d\uff0c\u5219\u4f1a\u4ea7\u751f\u7f16\u8bd1\u65f6\u9519\u8bef\u3002<\/span>
\n Java Switch\u8868\u8fbe\u5f0f\u5fc5\u987b\u4e3a byte\uff0cshort\uff0cint\uff0clong(\u5177\u6709Wrapper\u7c7b\u578b)\uff0c\u679a\u4e3e\u548c\u5b57\u7b26\u4e32<\/em>\u3002<\/span>
\n \u6bcf\u4e2acase\u8bed\u53e5\u53ef\u4ee5\u6709\u4e00\u4e2a break\u8bed\u53e5<\/em>\uff0c\u8fd9\u662f\u53ef\u9009\u7684\u3002\u5f53\u63a7\u4ef6\u5230\u8fbebreak\u8bed\u53e5\u65f6\uff0c\u5b83\u5c06\u5728switch\u8868\u8fbe\u5f0f\u540e\u8df3\u8f6c\u63a7\u4ef6\u3002\u5982\u679c\u672a\u627e\u5230break\u8bed\u53e5\uff0c\u5219\u6267\u884c\u4e0b\u4e00\u79cd\u60c5\u51b5\u3002<\/span>
\n \u6848\u4f8b\u503c\u53ef\u4ee5\u5177\u6709\u9ed8\u8ba4\u6807\u7b7e<\/em>\uff0c\u8be5\u6807\u7b7e\u662f\u53ef\u9009\u7684\u3002<\/span> <\/p>\n

\n \u8bed\u6cd5: <\/strong>\n <\/div>\n
\n
 switch(expression){
    case <\/span>value1:
    \/\/code to be executed;
    break;
    \/\/optionalcase <\/span>value2:
    \/\/code to be executed;
    break;
    \/\/optional...... default: code to be executed if <\/span>all cases are not matched;
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java Switch\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>SwitchExample {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/Declaring a variable for <\/span>switch <\/span>expression int number=20;
        \/\/switch <\/span>expression switch(number){
            \/\/case <\/span>statements case <\/span>10: System.out.println<\/span>(\"10\"<\/span>);
            break;
            case <\/span>20: System.out.println<\/span>(\"20\"<\/span>);
            break;
            case <\/span>30: System.out.println<\/span>(\"30\"<\/span>);
            break;
            \/\/default <\/span>case <\/span>statement default:System.out.println<\/span>(\"Not in 10, 20 or 30\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 20
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u67e5\u627e\u6708\u4efd\u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>SwitchMonthExample {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/Specifying month number int month=7;
        String monthString=\"\"<\/span>;
        \/\/switch <\/span>statement switch(month){
            \/\/case <\/span>statements within the switch <\/span>block case <\/span>1: monthString=\"1 - January\"<\/span>;
            break;
            case <\/span>2: monthString=\"2 - February\"<\/span>;
            break;
            case <\/span>3: monthString=\"3 - March\"<\/span>;
            break;
            case <\/span>4: monthString=\"4 - April\"<\/span>;
            break;
            case <\/span>5: monthString=\"5 - May\"<\/span>;
            break;
            case <\/span>6: monthString=\"6 - June\"<\/span>;
            break;
            case <\/span>7: monthString=\"7 - July\"<\/span>;
            break;
            case <\/span>8: monthString=\"8 - August\"<\/span>;
            break;
            case <\/span>9: monthString=\"9 - September\"<\/span>;
            break;
            case <\/span>10: monthString=\"10 - October\"<\/span>;
            break;
            case <\/span>11: monthString=\"11 - November\"<\/span>;
            break;
            case <\/span>12: monthString=\"12 - December\"<\/span>;
            break;
            default:System.out.println<\/span>(\"Invalid Month!\"<\/span>);
        }
        \/\/Printing month of the given number System.out.println<\/span>(monthString);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 7 - July
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u68c0\u67e5\u5143\u97f3\u6216\u8f85\u97f3\u7684\u7a0b\u5e8f: <\/strong>\n <\/div>\n
\n \u5982\u679c\u5b57\u7b26\u662fA\uff0cE\uff0cI\uff0cO\u6216U\uff0c\u5219\u4e3a\u5143\u97f3\uff0c\u5426\u5219\u4e3a\u8f85\u97f3\u3002\u5b83\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002\n <\/div>\n
\n
 public <\/span>class <\/span>SwitchVowelExample {
    public <\/span>static void <\/span>main(String[] args) {
        char ch='O'<\/span><\/span>;
        switch(ch) {
            case <\/span>'a'<\/span>: System.out.println<\/span>(\"Vowel\"<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>);
            break;
            case <\/span>'e'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'i'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'o'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'u'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'A'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'E'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'I'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'O': System.out.println<\/span>(\"Vowel\");
            break;
            case <\/span>'U'<\/span>: System.out.println<\/span>(\"Vowel\");
            break;
            default: System.out.println<\/span>(\"Consonant\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 Vowel
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java Switch\u8bed\u53e5\u662f\u76f4\u901a\u7684<\/h2>\n
\n Java switch\u8bed\u53e5\u662f\u76f4\u901a\u7684\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u6ca1\u6709break\u8bed\u53e5\uff0c\u5b83\u5c06\u5728\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u4e4b\u540e\u6267\u884c\u6240\u6709\u8bed\u53e5\u3002\n <\/div>\n
\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>SwitchExample2 {
    public <\/span>static void <\/span>main(String[] args) {
        int number=20;
        \/\/switch <\/span>expression with int value switch(number){
            \/\/switch <\/span>cases without break<\/span> statements case <\/span>10: System.out.println<\/span>(\"10\"<\/span>);
            case <\/span>20: System.out.println<\/span>(\"20\"<\/span>);
            case <\/span>30: System.out.println<\/span>(\"30\"<\/span>);
            default:System.out.println<\/span>(\"Not in 10, 20 or 30\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 2030Not in 10, 20 or 30
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5e26\u6709\u5b57\u7b26\u4e32\u7684Java Switch\u8bed\u53e5<\/h2>\n
\n \u81eaJava SE 7\u5f00\u59cb\uff0cJava\u5141\u8bb8\u6211\u4eec\u5728Switch\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u3002case\u8bed\u53e5\u5e94\u4e3a\u5b57\u7b26\u4e32\u6587\u5b57\u3002\n <\/div>\n
\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>SwitchStringExample {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/Declaring String variable String levelString=\"Expert\"<\/span><\/span>;
        int level=0;
        \/\/Using String in switch <\/span>expression switch(levelString){
            \/\/Using String Literal in switch <\/span>case <\/span> case <\/span>\"Beginner\"<\/span>: level=1;
            break;
            case <\/span>\"Intermediate\"<\/span>: level=2;
            break;
            case <\/span>\"Expert\": level=3;
            break;
            default: level=0;
            break;
        }
        System.out.println<\/span>(\"Your Level is: \"<\/span>+level);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 Your Level is: 3
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java\u5d4c\u5957Switch\u8bed\u53e5<\/h2>\n
\n \u6211\u4eec\u53ef\u4ee5\u5728Java\u4e2d\u7684\u5176\u4ed6switch\u8bed\u53e5\u4e2d\u4f7f\u7528switch\u8bed\u53e5\u3002\u8fd9\u5c31\u662f\u5d4c\u5957\u7684switch\u8bed\u53e5\u3002\n <\/div>\n
\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>NestedSwitchExample {
    public <\/span>static void <\/span>main(String args[]) {
        \/\/C - CSE, E - ECE, M - Mechanical char <\/span>branch = 'C'<\/span><\/span><\/span><\/span>;
        int collegeYear = 4;
        switch( collegeYear ) {
            case <\/span>1: System.out.println<\/span>(\"English, Maths, Science\"<\/span>);
            break;
            case <\/span>2: switch( branch ) {
                case <\/span>'C': System.out.println<\/span>(\"Operating System, Java, Data Structure\"<\/span>);
                break;
                case <\/span>'E'<\/span><\/span><\/span>: System.out.println<\/span>(\"Micro processors, Logic switching theory\"<\/span>);
                break;
                case <\/span>'M'<\/span><\/span><\/span>: System.out.println<\/span>(\"Drawing, Manufacturing Machines\"<\/span>);
                break;
            }
            break;
            case <\/span>3: switch( branch ) {
                case <\/span>'C': System.out.println<\/span>(\"Computer Organization, MultiMedia\"<\/span>);
                break;
                case <\/span>'E': System.out.println<\/span>(\"Fundamentals of Logic Design, Microelectronics\"<\/span>);
                break;
                case <\/span>'M': System.out.println<\/span>(\"Internal Combustion Engines, Mechanical Vibration\"<\/span>);
                break;
            }
            break;
            case <\/span>4: switch( branch ) {
                case <\/span>'C': System.out.println<\/span>(\"Data Communication and Networks, MultiMedia\"<\/span>);
                break;
                case <\/span>'E': System.out.println<\/span>(\"Embedded System, Image Processing\"<\/span>);
                break;
                case <\/span>'M': System.out.println<\/span>(\"Production Technology, Thermal Engineering\"<\/span>);
                break;
            }
            break;
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 Data Communication and Networks, MultiMedia
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Switch\u8bed\u53e5\u4e2d\u7684Java\u679a\u4e3e<\/h2>\n
\n Java\u5141\u8bb8\u6211\u4eec\u5728switch\u8bed\u53e5\u4e2d\u4f7f\u7528\u679a\u4e3e\u3002\n <\/div>\n
\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>enum Day {
    Sun, Mon, Tue, Wed, Thu, Fri, Sat }
    public <\/span>static void <\/span>main(String args[]) {
        Day[] DayNow = Day.values<\/span>();
        for <\/span>(Day Now : DayNow) {
            switch <\/span>(Now) {
                case <\/span>Sun: System.out.println<\/span>(\"Sunday\"<\/span>);
                break;
                case <\/span>Mon: System.out.println<\/span>(\"Monday\"<\/span>);
                break;
                case <\/span>Tue: System.out.println<\/span>(\"Tuesday\"<\/span>);
                break;
                case <\/span>Wed: System.out.println<\/span>(\"Wednesday\"<\/span>);
                break;
                case <\/span>Thu: System.out.println<\/span>(\"Thursday\"<\/span>);
                break;
                case <\/span>Fri: System.out.println<\/span>(\"Friday\"<\/span>);
                break;
                case <\/span>Sat: System.out.println<\/span>(\"Saturday\"<\/span>);
                break;
            }
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 SundayMondayTwesdayWednesdayThursdayFridaySaturday
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Switch\u8bed\u53e5\u4e2d\u7684Java\u5305\u88c5\u5668<\/h2>\n
\n Java\u5141\u8bb8\u6211\u4eec\u5728switch\u8bed\u53e5\u4e2d\u4f7f\u7528\u56db\u4e2a\u5305\u88c5\u5668\u7c7b: Byte\uff0cShort\uff0cInteger\u548cLong\u3002\n <\/div>\n
\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>WrapperInSwitchCaseExample {
    public <\/span>static void <\/span>main(String args[]) {
        Integer age = 18;
        switch <\/span>(age) {
            case <\/span>(16): System.out.println<\/span>(\"You are under 18.\"<\/span>);
            break;
            case <\/span>(18): System.out.println<\/span>(\"You are eligible for <\/span>vote.\"<\/span>);
            break;
            case <\/span>(65): System.out.println<\/span>(\"You are senior citizen.\"<\/span>);
            break;
            default: System.out.println<\/span>(\"Please give the valid age.\"<\/span>);
            break;
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 You are eligible for <\/span>vote.
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java Switch\u8bed\u53e5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-705","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/705"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=705"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}