; import java.util.Calendar; import java.util.Scanner; public class Main2 { public static void main(String[] args) { // Calendar calendar = Calendar.getInstance(); Scanner sc = new Scanner(System.in); // System.out.print("请输入年份"); // System.out.print("请输入月份"); int year = sc.nextInt(); int month = sc.nextInt(); int days = DaysReturn(year, month); Print(year, days, month); } static boolean judge(int year) {// 判断是不是闰年 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; else return false; } static int DaysReturn(int year, int month)// 找出输入的日期的月份的一号距离2007年1月1日有多少天 { int sum = 0;// 统计一年内的天数 int sum1 = 0;// 统计间隔了多少年的天数 if (judge(year)) { switch (month) { case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 31 + 29; break; case 4: sum = 31 + 29 + 31; break; case 5: sum = 31 + 29 + 31 + 30; break; case 6: sum = 31 + 29 + 31 + 30 + 31; break; case 7: sum = 31 + 29 + 31 + 30 + 31 + 30; break; case 8: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31; break; case 9: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31; break; case 10: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30; break; case 11: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 12: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; } } else { switch (month) { case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 31 + 28; break; case 4: sum = 31 + 28 + 31; break; case 5: sum = 31 + 28 + 31 + 30; break; case 6: sum = 31 + 28 + 31 + 30 + 31; break; case 7: sum = 31 + 28 + 31 + 30 + 31 + 30; break; case 8: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31; break; case 9: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31; break; case 10: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30; break; case 11: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 12: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; } } for (int i = 2007; i < year; i++) { if (judge(i)) { sum1 += 366; } else sum1 += 365; } sum1 += sum;// 总天数 return sum1; } static void Print(int year, int days, int month) { int x = days % 7 + 1;// 找出这个月的1号是星期几 int monthdays;// 这个月有多少天 System.out.println("---------------------"); System.out.println(" Su Mo Tu We Th Fr Sa"); System.out.println("---------------------"); if (month == 2) { if (judge(year)) monthdays = 29; else monthdays = 28; } else if (month == 4 || month == 9 || month == 6 || month == 11) { monthdays = 30; } else monthdays = 31; if (x == 7) System.out.printf("%3d", 1);// 针对每个月一号不同的星期制定不同的输出格式 if (x == 1) System.out.printf("%6d", 1); if (x == 2) System.out.printf("%9d", 1); if (x == 3) System.out.printf("%12d", 1); if (x == 4) System.out.printf("%15d", 1); if (x == 5) System.out.printf("%18d", 1); if (x == 6) System.out.printf("%21d", 1); for (int i = 2; i <= monthdays; i++) { System.out.printf("%3d", i); if ((i + (x % 7)) % 7 == 0 && i < monthdays) System.out.printf("\n");// 注意如果一个月的最后一号是星期6则无需空行 } System.out.println(); System.out.println("---------------------"); } }
解题思路:
注意事项:
参考代码:
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:677 |
数列排序 (C语言代码)浏览:858 |
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)for循环浏览:1178 |
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:443 |
这可能是一个假的冒泡法浏览:1071 |
WU-蓝桥杯算法提高VIP-勾股数 (C++代码)浏览:1685 |
WU-小九九 (C++代码)浏览:1713 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:513 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:1968 |
C二级辅导-公约公倍 (C语言代码)浏览:537 |