1.根据输入的年份和月份,打印出当月的日历
import java.util.Scanner;
public class Demo13 {
public static void main(String[] args) {
System.out.println("输入年份");
Scanner w=new Scanner(System.in);
int year=w.nextInt();
System.out.println("输入月份");
int month=w.nextInt();
System.out.println(year+"年份"+month+"月份");
int monthDays;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
monthDays=31;
}else if(month==4||month==6||month==9||month==11){
monthDays=30;
}else{
if((year%4==0&&year%100!=0)||year%400==0){
monthDays=29;
}else{
monthDays=28;
}
}
int sumYear=0;
for(int i=1900;i<year;i++) {
if ((i % 4 == 0 && i % 100 != 0 )|| i % 400 == 0) {
sumYear += 366;
} else {
sumYear += 365;
}
}
int sumMonth=0;
for(int i=1;i<month;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
sumMonth+=31;
}else if(i==4||i==6||i==9||i==11){
sumMonth+=30;
}else {
if(year%4==0&&year%100!=0||year%400==0){
sumMonth+=29;
} else {
sumMonth += 28;
}
}
}
int week=(sumMonth+sumYear+1)%7;
System.out.println("一\t二\t三\t四\t五\t六\t日");
for(int i=1;i<=week-1;i++) {
System.out.print("\t");
}
for(int i=1;i<=monthDays;i++){
System.out.print(i+"\t");
if((sumMonth+sumYear+i)%7==0){
System.out.println();
}
}
}
}