下列程序段的输出结果为【 】。 int a=2; switch (A){ case 2: System. out. print( "Value is 2."); case 3 : System. out. println( "Value is 3. ")

admin2010-12-17  21

问题 下列程序段的输出结果为【  】。
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语句后退出。
转载请注明原文地址:https://kaotiyun.com/show/B49p777K
0

最新回复(0)