欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

switch case执行顺序

程序员文章站 2023-03-28 08:35:11
public class SwitchCase { public static void main(String[] args) { System.out.println(switchFun(4)); //运行结果:8 } public static int switchFun(int x){ in... ......
public class switchcase {
    public static void main(string[] args) {
        system.out.println(switchfun(4));  //运行结果:8
    }
    
    public static int switchfun(int x){
        int j = 1;
        switch (x) {
        case 1:
            j++;
        case 2:
            j++;
        case 3:
            j++;
        case 4:  //输入x=4,从此处开始执行,一直到末尾,default里面的内容也会执行
            j++; //由于j++是单独一行,等效于j = j + 1,不存在先使用后+1的情况
        case 5:
            j++;
        default:
            j++;
        }
        return x+j;
    }
}