import java.util.Scanner; // 导入Scanner类
public class Exl_6202252320205 {
public static void main(String[] args) {
// 创建一个Scanner对象,用于接收键盘输入
Scanner sc = new Scanner(System.in);
// 提示用户输入年份
System.out.println("请输入年份:");
// 读取用户输入的整数,存储到year变量中
int year = sc.nextInt();
// 判断年份是否合法
if (year <= 0) {
// 如果不合法,输出错误信息,并结束程序
System.out.println("年份必须是一个正整数");
return;
}
// 提示用户输入月份
System.out.println("请输入月份:");
// 读取用户输入的整数,存储到month变量中
int month = sc.nextInt();
// 判断月份是否合法
if (month < 1 || month > 12) {
// 如果不合法,输出错误信息,并结束程序
System.out.println("月份必须是一个1到12之间的整数");
return;
}
// 关闭Scanner对象
sc.close();
// 声明一个变量,存储该月的天数
int days = 0;
// 根据月份的不同,赋值不同的天数
switch (month) {
case 1: // 一月有31天
case 3: // 三月有31天
case 5: // 五月有31天
case 7: // 七月有31天
case 8: // 八月有31天
case 10: // 十月有31天
case 12: // 十二月有31天
days = 31;
break;
case 4: // 四月有30天
case 6: // 六月有30天
case 9: // 九月有30天
case 11: // 十一月有30天
days = 30;
break;
case 2: // 二月根据闰年判断是否有29天或28天
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days = 29; // 如果是闰年,二月有29天
} else {
days = 28; // 如果不是闰年,二月有28天
}
break;
}
// 输出该月的天数的结果
System.out.println(year + "年" + month + "月有" + days + "天");
}
}