下列代码运行结果是( )

public class Test125 {
    public static void main(String argv[]) {
        System.out.println(switchlt(4));
    }
    public static int switchlt(int x) {
        int j = 1;
        switch (x) {
        case 1:
            j++;
        case 2:
            j++;
        case 3:
            j++;
        case 4:
            j++;
        case 5:
            j++;
        default:
            j++;
        }
        return j + x;
    }
}
A.3     B.4     C.5     D.6     E.7     F.8

参考解答

F

分析

程序执行时,传入的数值为x=4,会匹配到switch分支的case 4,由于没有break语句,方法会继续执行 case 5 和default j++ 3次后j=4,加 x 等于8。


results matching ""

    No results matching ""