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

admin2010-07-28  24

问题 有如下程序    public class Sun    {      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=0,b=0

答案1

解析 该题考查对switch-case-break语句的理解。该题x=1与case 1语句匹配,执行a++语句,a的值为1,而case 1语句后没有break语句,所以程序继续向下执行case 2语句,a++, b++,a的值为2,b的值为1。故本题答案选项是A。
转载请注明原文地址:https://jikaoti.com/ti/rfH0FFFM
0

最新回复(0)