• Java switch的用法


    控制流程语句之→switch选择判断语句

     注意事项

    1、多个case后面的值不能重复;

    2、switch后面小括号当中只能是下列数据类型:

      基本数据类型:byte、short、char、int

      引用数据类型:String字符串、enum枚举

    3、switch语句格式化可以很灵活:前后顺序可以颠倒,而且break语句还可以省略不写。

    “匹配到哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束为止。”

    package codeJudge;
    
    public class demo {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Test();
            defaultTest();
            StringTest();
            breakTest();
        }
        
        / * 系统的switch
         * 输出数据不合理
         * */
        private static void Test(){
            int num=11;
            switch(num) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期七");
                break;
            default:
                System.out.println("数据不合理!");
                break;
            }
        }
        
        /*default 不是必须的,也可以不写 */
        private static void defaultTest() {
            char ch='C';
            switch (ch) {
            case 'B':
                System.out.println("case one");
                break;
            case 'A':
                System.out.println("case two");
                break;
            case 'C':
                System.out.println("case three");
                break;
            }
        }
        
        /*判断String类型
         * 输出OK
         */
        private static void StringTest() {
            String string= new String("hello");
            switch (string) {
            case "hello":
                System.out.println("OK");
                break;
            default :
                System.out.println("ERROR");
                break;
            }
        }
        
        /*case语句中不写break,编译并不会报错
         * 会一直执行之后的所有case条件下的语句,并不再进行判断,直到default语句
         * 代码输出结果为:     case two 
         *                 case three
         *                 测试结束!
         */
        private static void breakTest() {
            char ch='B';
            switch (ch) {
            case 'A':
                System.out.println("case one");
            case 'B':
                System.out.println("case two");
            case 'C':
                System.out.println("case three");
            default :
                System.out.println("测试结束!");
                break;
            }
        }
    }
  • 相关阅读:
    sqlhelper类
    嵌入式的n个方向
    study vim + cscope
    mail lists
    关于我的学习
    yahoo enter linux mobile competition
    找工作啦 啦啦啦啦啦
    minicom display unsolicited codes
    并购的年代
    配置rt73无线网卡至suse10.3
  • 原文地址:https://www.cnblogs.com/tisnk/p/12335267.html
Copyright © 2020-2023  润新知