Java Switch 语句案例解析:输出季节信息
Java Switch 语句案例解析:输出季节信息
代码示例:
int season = 2;
switch(season) {
case 1: System.out.print('春天');
break;
case 2: System.out.println('夏天');
case 3: System.out.println('秋天');
break;
case 4: System.out.println('冬天');
default: System.out.println('有误');
}
代码分析:
- 代码使用
switch语句根据season变量的值来执行不同的代码块。 - 当
season为 2 时,执行case 2的代码块,输出'夏天'。 - 由于
case 2和case 3之间没有break语句,程序会继续执行case 3的代码块,输出'秋天'。 case 4的代码块没有被执行,因为season的值为 2。default代码块用于处理没有匹配case的情况,但由于season的值为 2,所以default代码块也被执行,输出'有误'。
输出结果:
'夏天' '秋天' '有误'
正确答案:D. 夏天秋天冬天有误
原文地址: https://www.cveoy.top/t/topic/vuQ 著作权归作者所有。请勿转载和采集!