import java.util.Scanner;
public class Test5_29 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the year");
int year = input.nextInt();
System.out.print("Represents the day of the week on which the first day of the year falls");
int dayOfWeek = input.nextInt();
// 计算当月总天数
int days;
int month;
String Month = " ";
boolean b =year%4==0&&year%100!=0||year%400== 0;
for ( month = 1; month <= 12; month++) {
if (month == 2) {
if (b) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
switch (month) {
case 1 -> Month = "January";
case 2 -> Month = "February";
case 3 -> Month = "March";
case 4 -> Month = "April";
case 5 -> Month = "May";
case 6 -> Month = "June";
case 7 -> Month = "July";
case 8 -> Month = "August";
case 9 -> Month = "September";
case 10 -> Month = "October";
case 11 -> Month = "November";
case 12 -> Month = "December";
}
// 3.打印当月日历
System.out.println("\t\t" + Month + " " + year + "\t\t");
System.out.println("———————————————————————————————————");
System.out.println("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\t");
switch (dayOfWeek) {
case 1 -> System.out.print("\t");
case 2 -> System.out.print("\t\t");
case 3 -> System.out.print("\t\t\t");
case 4 -> System.out.print("\t\t\t\t");
case 5 -> System.out.print("\t\t\t\t\t");
case 6 -> System.out.print("\t\t\t\t\t\t");
case 7 -> System.out.print("");
}
//如果是星期六,换行
for (int k = 1; k <= days; k++) {
if (k % 7 == 7-dayOfWeek) {
System.out.println("\t"+k+"\t");
} else {
System.out.print( "\t"+k );
}
}
System.out.println();//打印完一个月之后换行
dayOfWeek = dayOfWeek + days % 7;//计算下一个月第一天是星期几
}
}
}
改最后一行代码就可以了
// 比如2022年一月是31天第一天是星期六 那么下个月就是 ((31 % 7) + 6) % 7 = 星期2
dayOfWeek = (days%7 + dayOfWeek) % 7;