Java 基础进阶 12 -switch-case


1,数据变量的值,选择相应的 case 去判断,一旦满足 case 条件,就执行 case 的相应语句;如果没有 break 或者已经到结尾的话,会继续执行其下的 case 语句。

2,default:是可选的,而且位置是灵活的。

3,变量可以是哪些类型? char byte short int 枚举 String(jdk1.7);

4,case 条件:其中条件只能是值,不能是取值范围!


public class TestSwitch1 {
    public static void main(String[] args) {
        int i = 2;
        switch (i) {

            case 0:
                System.out.println("zero");
                break;
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            case 3:
                System.out.println("three");
                break;
            default:
                System.out.println("other");
        }
    }
}



猜你喜欢

转载自blog.csdn.net/u010282984/article/details/80737214