java的switch用法,穿透现象

java中switch的用法和逻辑运算符

package test;

public class SwitchTest {
    public static void main(String[] args) {
        String strs = "a";
        switch (strs) {
            case "a":
                if (strs.length() == 1) {
                    System.out.println(1);
                   // break;
                }
                if (strs.equals("a")) {
                    System.out.println("a");
                    break;
                }
            case "b":
                System.out.println("b");

        }

        char c = 'A' ;
        int num = 10 ;
        switch(c) {
            case 'B' :
                num ++ ;
                System.out.println("B");
                System.out.println("num1 = " + num);
            case 'A' :
                num ++ ;
                System.out.println("A");
                System.out.println("num2 = " + num);
            case 'Y' :
                num ++ ;
                System.out.println("Y");
                System.out.println("num3 = " + num);
                break ;
            default :
                num -- ;
                System.out.println("num4 = " + num);
        }
        System.out.println(num) ;


        getValue(1);
    }
    public static int getValue(int i) {
        int result = 0;
        switch (i) {
            case 1:
                result = result + i;
                System.out.println("result1 = " + result);
            case 2:
                result = result + i * 2;
                System.out.println("result2 = " + result);
              //  break;
            case 3:
                result = result + i * 3;
                System.out.println("result3 = " + result);
        }
        return result;
    }

}

java的switch用法,穿透现象_第1张图片

你可能感兴趣的:(java,jvm,开发语言)