用Java利用switch语句实现键盘输入 月份,打印输出该月份对应的 季节(春、夏、秋、冬)。
时间: 2023-04-01 10:04:03 浏览: 211
可以使用以下代码实现:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入月份:");
int month = scanner.nextInt();
String season;
switch (month) {
case 1:
case 2:
case 12:
season = "冬季";
break;
case 3:
case 4:
case 5:
season = "春季";
break;
case 6:
case 7:
case 8:
season = "夏季";
break;
case 9:
case 10:
case 11:
season = "秋季";
break;
default:
season = "输入月份有误";
break;
}
System.out.println("该月份对应的季节为:" + season);
}
}
阅读全文