import java.util.Scanner;
public class index {
// 每个月的天数
public static int monthday(int month, int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
int[] day = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return day[month];
} else {
int[] day = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return day[month];
}
}
// 月份总天数
public static int monthdays(int month, int year) {
int totaldays = 0;
for (int i = 1; i < month; i++) {
totaldays = totaldays + monthday(i, year);
}
return totaldays;
}
// 距离 1900 年的年份总天数
public static int yeardays(int year){
int yeardays = 0;
for (int i = 1900;i
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
yeardays = yeardays+366;
} else {
yeardays = yeardays+365;
}
}
System.out.println(year+" 年距离 1900 年的总天数 "+yeardays);
return yeardays;
}
// 输出日历
public static void printCalendar(int month,int year){
int totaldays = 0;
if (year > 0) {
if (month > 0 && month < 13) {
// 距离 1900 年 1 月 1 日总天数
totaldays = monthdays(month,year)+yeardays(year);
System.out.println(year+" 年 "+month+" 月 1 日距离 1900 年的总天数 :"+totaldays);
System.out.外汇跟单gendan5.comprintln("\n**********"+year+" 年 "+month+" 月的日历为 **********");
System.out.println(" 一 \t 二 \t 三 \t 四 \t 五 \t 六 \t 日 \t");
int week = 1+totaldays%7;
// 根据 1 日为周几输出空格
for(int i=1;i
System.out.print(" \t");
}
// 输入具体日期
for(int i=1;i<=monthday(month,year);i++){
System.out.print(i+"\t");
if(week==7){
week = 1;// 重置为星期一
System.out.println();
}else{
week++;
}
}
} else {
System.out.println(" 输入的月份不合法! ");
}
} else {
System.out.println(" 输入的年份不合法! ");
}
}
// 主函数
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("*********************** 欢迎使用万年历 **************************");
System.out.println("********* 请选择你需要进行的操作(输入进行操作之前的数字) **********");
System.out.println("********************1. 查询某年某月的日历 ************************");
System.out.println("********************2. 结束操作 *********************************");
System.out.print("\n 请选择你需要进行的操作: ");
int a = scanner.nextInt();
for (int i=0;i>=0;i++) {
switch (a) {
case 1:
System.out.print(" 请选择年份: ");
int year = scanner.nextInt();
System.out.print(" 请选择月份: ");
int month = scanner.nextInt();
printCalendar(month, year);
System.out.print("\n 请选择你需要进行的操作: ");
a = scanner.nextInt();
break;
case 2:
System.out.println(" 退出程序成功! ");
return;
}
}
}
}