Java 入门7 if switch switch 穿透 以及switch穿透 解决代码冗余的问题

if 分支语句 三种

 

 switch分支语句

 

 

public class Demo {
    public static void main(String[] args) {
        //if 分支语句
        int a = 11;
        if (a >= 20) {
            System.out.println("a大于20");
        } else if (a == 15) {
            System.out.println("a小鱼15");
        } else {
            System.out.println("a小鱼15");
        }

        //switch 语句
        String Day = "周五";
        switch (Day) {
            case "周一":
                System.out.println("今天礼拜一");
                break;
            case "周二":
                System.out.println("礼拜二");
                break;
            case "周三":
                System.out.println("礼拜三");
                break;
            case "周四":
                System.out.println("礼拜四");
                break;
            case "周五":
                System.out.println("今天星期五");
                break;
            default:
                System.out.println("休息");
        }
    }
}
View Code

 

 

 

 

public class Demo1 {
    public static void main(String[] args) {
        //目标 清楚switch的注意点 并在开发时注意
        // 表达式类型只能使byte short int  char JDK5开发支持的枚举,不支持double float long

        double a=0.1+0.2;//小数运算可能是不精确的
        System.out.println(a);

//        long lg=20;
//        switch(lg){
//
//        }

        //case 给出的值不允许重复, 且只能是字面量  不能是变量
        switch(3){
            case 31:
                break;
//            case a:break;
        }

        //不要忘记break  否则可能会出现穿透现象
    }
}
View Code

 

posted @ 2022-06-20 15:21  还有什么值得拥有  阅读(57)  评论(0编辑  收藏  举报