题目:设计一个日历的程序
输入格式:
请输入年份:
2022
请输入月份:
1
输出格式:
这个月的第一天是:6
一 二 三 四 五 六 日
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入年份:");
int year = input.nextInt();
System.out.println("请输入月份:");
int month = input.nextInt();
int days = 0;
for (int i = 1900; i < year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
days += 366;
}else{
days += 365;
}
}
for (int i = 1; i < month; i++) {
if (i == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days += 29;
}
} else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
days += 31;
} else {
days += 30;
}
}
int week = 1 + days % 7;
System.out.println("这个月的第一天是:" + week);
System.out.println("一\t二\t三\t四\t五\t六\t日\t");
for (int i = 1; i < week; i++) {
System.out.print("\t");
}
int damon = 0;
switch (month) {
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
damon = 29;
} else {
damon = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
damon = 30;
break;
default:
damon = 31;
break;
}
for (int i = 1; i <= damon; i++) {
System.out.print(i + " \t");
if ((week - 1 + i) % 7 == 0) {
System.out.println();
}
}
}
}