1.用for循环、switch
以1990.01.01作为参考
public class Demo_9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("输入年、月(如:2020 01):");
int year = scan.nextInt();
int month = scan.nextInt();
int daysum = 0; // 天数加和
int week = 0; //初始化星期
//判断闰年,并将每年的天数相加
for (int i = 1990; i < year; i++) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
daysum += 366;
} else {
daysum += 365;
}
}
//用循环将最后一年每个月天数相加
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysum += 31;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
daysum += 29;
break