执行如下程序: public class Test { public static void main (String args[]) { int x=1,a=0,b=0; switch (x

admin2010-12-17  31

问题 执行如下程序:    public class Test {        public static void main (String args[]) {            int x=1,a=0,b=0;            switch (x) {                case 0: b++;                case 1: a++;                case 2: a++;b++;            }            System.out.println("a=" +a ",b=" +b);        }    }    该程序的输出结果是(  )。

选项 A、a=2,b=1
B、a=1,b=1
C、a=1,b=0
D、a=2,b=2

答案A

解析 本题关键是要搞清楚该程序执行了哪几条语句。由于x的值等于1,所以switch结构中,程序从case1后面的语句开始执行,又因为case1后面的语句没有以break结束,所以程序要继续往下执行case 2后面的语句。所以,该程序共执行了这3条语句:a++;a++;b++;因此,变量a和b最后的值应该为2和1。
转载请注明原文地址:https://jikaoti.com/ti/mPH0FFFM
0

随机试题
最新回复(0)