[填空题] 下列程序段的输出结果为 【8】 。int a=2;switch (a) case 2:System. out. print( "Value is
2021-07-20
[填空题] 下列程序段的输出结果为 【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语句后退出。
词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。
