import java.util.Scanner;
class Calender{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int sum=0;
System.out.println("请输入年份");
int year=input.nextInt();
System.out.println("请输入月份");
int month=input.nextInt();
System.out.println("---------------------" + year + "年" + month + "月" + "---------------------");
for (int i=1900;i<year ; i++ ) { //判断每一年的天数
if((i%4==0 && i%100!=0 )|| (i%400==0)) {
sum += 366; // 闰年366天
}else {
sum += 365; // 平年365天
}
}
//对12个月进行循环
for(int i=1;i<month;i++) {
if(i==2){
if(year%4==0 && year%100!=0 || year%400==0) {
sum += 29;
}else {
sum += 28;
}
}else {
if(i==4 || i==6 || i==9 || i==11) {
sum += 30;
}else {
sum += 31;
}
}
}
//判断每个月的天数
int day=0;
if(month==2) {
if(year%4==0 && year%100!=0 || year%400==0){
day=29;
}else {
day=28;
}
}else if(month==4 || month==6 || month==9 || month==11) {
day=30;
}else {
day=31;
}
//输出对应的星期
int week=(sum+1)%7;
//System.out.println(sum);
//System.out.println(week);
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int y=1;y<=week;y++){
System.out.print("\t");
}
for(int i=1;i<=day;i++) {
if((i+week)%7==0) {
System.out.print(i + "\n");
}else{
System.out.print(i + "\t");
}
}
}
} |
|