import java.util.Scanner;
public class Practice02 {
public static void main(String[] args) {
//输入年月,输出该月日历
Scanner input = new Scanner(System.in);
int year,month,monthDay=0,sumDay=1,weekDay;
System.out.print("请输入年份:");
year = input.nextInt();
for(int i=1900;i<year;i++){
sumDay+=(i%4==0 && i%100!=0 || i%400==0)?366:365;
}
计算输入的年份前一年至1900年的总天数
System.out.print("请输入月份:");
month = input.nextInt();
for(int i=1;i<=month;i++)
switch(i){
case 1: case 3: case 5:case 7: case 8: case 10:case 12:
monthDay = 31;
if(i<month){
sumDay+=monthDay;
}
break;
case 4: case 6: case 9:case 11:
monthDay = 30;
if(i<month){
sumDay+=monthDay;
}
break;
case 2:
monthDay = (year%4==0 && year%100!=0 || year%400==0)?29 : 28;
if(i<month){
sumDay+=monthDay;
}
break;
}
weekDay=(sumDay)%7;
计算输入年份已过去的天数(不包括输入的当月份),并确定输入月的1号是星期几
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=0;i<weekDay;i++){
System.out.print("\t");
}
for(int i=1;i<=monthDay;i++){
System.out.print(i+"\t");
if(sumDay%7==6){
System.out.println();
}
sumDay++;
}
}
}
先判断该月的具体天数,然后按照日历格式输出