输入一个年份,一个月份,判断这个月份有多少天。
package HU;
import java.util.*;
public class HU {
public static void main(String[] args) {
// 用于判断平闰年,平年为0,闰年为1
int judge = 0;
// 输入年份year,月份month
Scanner input = new Scanner(System.in);
int year = input.nextInt();
int month = input.nextInt();
// 输出所输入的年份和月份
System.out.println("年份:" + year);
System.out.println("月份:" + month);
// 判断平闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
judge = 1;
}
// 对月份判断并输出相应月份的天数,遇到平闰年需额外判断2月
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
System.out.println("31天");
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
System.out.println("30天");
} else if (month == 2 && judge == 1) {
System.out.println("29天");
} else if (month == 2 && judge == 0) {
System.out.println("28天");
}
}
}