9-分支选择

if判断
在Java程序中,如果要根据条件来决定是否执行某一段代码,就需要if语句
if(条件){//语句块1
    //条件满足执行此语句块1
}else{//语句块2
    //条件不满足时执行 else 下的语句块2
    //else 不是必须的 
    //当不存在 else ,如果条件不满足,将会跳过语句块1
}

注意:不建议省略 {}
串联if-else
        int n = 100;
        if (n >= 60) {//条件1
            System.out.println("及格了");
        } else if (n >= 90) {//条件2
            System.out.println("优秀");
        } else {//条件1和2都不满足
            System.out.println("挂科了");
        }
//当条件有多个时可以将if else 串联使用      

注意:串联使用多个 if 时,要特别注意判断顺序,还要特别注意边界条件(> 和 >= 效果是不同的)
浮点数在计算机中常常无法精确表示,计算可能出现误差,因此,判断浮点数相等用==判断不靠谱
        double x = 1 - 9.0 / 10;
        if (Math.abs(x - 0.1) < 0.00001) {//浮点数判断
            System.out.println("x is 0.1");
        } else {
            System.out.println("x is NOT 0.1");
        }

判断引用类型
在Java中,判断值类型的变量是否相等,可以使用==运算符。但是,判断引用类型的变量是否相等,== 表示是否指向同一个对象。
        String s1 = "hello";
        String s2 = "HELLO".toLowerCase();//大写转小写
        System.out.println(s1);
        System.out.println(s2);
        if (s1 == s2) {
            System.out.println("s1 == s2");
        } else {
            System.out.println("s1 != s2");
        }
/* 编译结果
    hello
    hello
    s1 != s2
    两个String,它们的内容是相同的,但分别指向不同的对象,结果为false  
*/         


要判断引用类型的变量内容是否相等,必须使用equals()方法:
        String s1 = "hello";
        String s2 = "HELLO".toLowerCase();
        System.out.println(s1);
        System.out.println(s2);
        if (s1.equals(s2)) {
            System.out.println("s1 == s2");
        } else {
            System.out.println("s1 != s2");
        }
/*编译结果
    hello
    hello
    s1 == s2
*/        

注意:执行语句s1.equals(s2)时,如果变量s1为null,会报NullPointerException:
        String s1 = null;
        if (s1.equals("hello")) {
            System.out.println("hello");
        }
// Exception in thread "main" java.lang.NullPointerException        

要避免NullPointerException错误,可以利用短路运算符&&,或者将一定不是null的对象放到前面:
        String s1 = null;
        if (s1 != null && s1.equals("hello")) {
            System.out.println("hello");
        }



switch多重选择
除了if语句外,还有一种条件判断,是根据某个表达式的结果,分别去执行不同的分支。
switch语句根据switch (表达式)计算的结果,跳转到匹配的case结果,然后继续执行后续语句,直到遇到break结束执行,当没有匹配到任何case时,执行最后的default。
        int option = 99;
        switch (option) {//条件
        case 1://匹配值
            System.out.println("Selected 1");
            break;
        case 2:
            System.out.println("Selected 2");
            break;
        case 3:
            System.out.println("Selected 3");
            break;
        default://匹配到任何case
            System.out.println("Not selected");
            break;
        }

注意:使用switch时,注意case语句并没有花括号{ } ,而且,case语句具有“穿透性”,漏写break将导致意想不到的结果。但是只要保证有break,case的顺序不影响程序逻辑。
//如果有几个case语句执行的是同一组语句块,可以这么写
        switch (option) {
        case 1:
            System.out.println("Selected 1");
            break;
        case 2://忽略 break
        case 3:
            System.out.println("Selected 2, 3");
            break;
        default:
            System.out.println("Not selected");
            break;
        }

switch语句还可以匹配字符串。字符串匹配时,是比较“内容相等”:
        String fruit = "apple";
        switch (fruit) {
        case "apple":
            System.out.println("Selected apple");
            break;
        case "pear":
            System.out.println("Selected pear");
            break;
        case "mango":
            System.out.println("Selected mango");
            break;
        default:
            System.out.println("No fruit selected");
            break;
        }

编译检查
使用IDE时,可以自动检查是否漏写了break语句和default语句。
在Eclipse中,选择Preferences - Java - Compiler - Errors/Warnings - Potential programming problems,将以下检查标记为Warning:
'switch' is missing 'default' case
'switch' case fall-through
当switch语句存在问题时,即可在IDE中获得警告提示。

Java12中的switch 
从Java 12开始,switch语句升级为更简洁的表达式语法,使用类似模式匹配(Pattern Matching)的方法,保证只有一种路径会被执行,并且不需要break语句:
        String fruit = "apple";
        switch (fruit) {
        case "apple" -> System.out.println("Selected apple");
        case "pear" -> System.out.println("Selected pear");
        case "mango" -> {
            System.out.println("Selected mango");
            System.out.println("Good choice!");
        }
        default -> System.out.println("No fruit selected");
        }

新语法使用 -> ,如果有多条语句,需要用 { } 括起来。不要写break语句,因为新语法只会执行匹配的语句,没有穿透效应,同时新的switch语法,可以直接返回值。
        String fruit = "apple";
        int opt = switch (fruit) {
            case "apple" -> 1;
            case "pear", "mango" -> 2;
            default -> 0;
        }; // 注意赋值语句要以;结束
        System.out.println("opt = " + opt);

注意:由于switch表达式是作为Java 12的预览特性实现的,编译的时候,还需要给编译器加上参数,这样才能正常编译。
javac --source 12 --enable-preview Main.java

 

posted @ 2019-08-27 11:44  年小猫  阅读(226)  评论(0编辑  收藏  举报