import java.util.Scanner;
public class MonthDays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Year:");
int year = sc.nextInt();
System.out.print("Enter Month:");
int month = sc.nextInt();
int Days = 31;
// 4,6,9,11月
if (month == 4 || month == 6 || month == 9 || month == 11) {
Days = 30;
}
// 平年或闰年二月的天数
if (month == 2) {
if (year % 4 != 0) {
Days = 28;
} else if (year % 100 == 0 & year % 400 != 0) {
Days = 28;
} else {
Days = 29;
}
}
System.out.println(year + "年" + month + "月有" + Days + "天。");
}
}
输出示例:
Enter Year:2018
Enter Month:8
2018年8月有31天。