@
目录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<year;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.println("\n**********"+year+"年"+month+"月的日历为**********"); System.out.println("一\t二\t三\t四\t五\t六\t日\t"); int week = 1+totaldays%7; //根据1日为周几输出空格 for(int i=1;i<week;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; } } } }
注意:我的周日是在最后一栏