[填空题] 下列程序段的输出结果为 【8】 。int a=2;switch (a) case 2:System. out. print( "Value is 2."); ca

11 查阅

[填空题] 下列程序段的输出结果为 【8】

int a=2;

switch (a)

case 2:

System. out. print( "Value is 2.");

case 3 :

System. out. println( "Value is 3. ");

break;

default:

System. out. println ("end ");

break;

参考答案:

Value is 2. Value is 3.

switch语句的表达式a将依次与case后的值进行匹配,如果遇到匹配的值,则执行该case子句后的语句序列。当表达式的值与任意一个case子句的值都不匹配时,执行default后的语句。break语句用来在执行完一个case子句后,使程序跳出switch语句。此题先执行case2后的语句再执行case 3后的语句,遇到case 3中的break语句后退出。

计算机考试