1 package com.bdqn.test1; 2 3 import java.util.Scanner; 4 5 public class WanNianLi { 6 7 Scanner input = new Scanner(System.in); 8 int year ;//年份 9 int month;//月份 10 int days = 31;//默认31天,所以判断时直接除了4,6,9,11,2, 11 12 /* 13 * 显示菜单 14 */ 15 public void menu(){ 16 System.out.println("请输入年"); 17 year = input.nextInt(); 18 System.out.println("请输入月份"); 19 month = input.nextInt(); 20 21 switch (month) { 22 case 4: 23 case 6: 24 case 9: 25 case 11: 26 days = 30; 27 break; 28 case 2: 29 // 如果 年能被4整除 但不能被100 整除 + 年被400整除 30 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 31 days = 29; 32 break; 33 } else { 34 days = 28; 35 } 36 break; 37 default: 38 days = 31; 39 } 40 show(); 41 } 42 43 public void show(){ 44 // 1900-1-1 星期一 1900-1-9(规定) 45 String[] weeks = { "日", "一", "二", "三", "四", "五", "六" }; 46 for (int i = 0; i < weeks.length; i++) { 47 System.out.print(weeks[i] + "\t"); 48 } 49 System.out.println("\n=================================="); 50 int chaDays = 0; 51 for (int i = 1900; i < year; i++) { 52 if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { 53 chaDays += 366;// 闰年366天 54 continue; 55 } 56 chaDays += 365; 57 } 58 //上面方法计算出从1900年1月1日到输入的年,下面方法为把输入的月加上 59 //上一个for加完chaDays,下面的for循环继续在chaDays上累加 60 //这里注意是for循环的把每个月的累加上 61 for (int i = 1; i < month; i++) { 62 switch (i) { 63 case 1: 64 case 3: 65 case 5: 66 case 7: 67 case 8: 68 case 10: 69 case 12: 70 chaDays += 31; 71 break; 72 case 4: 73 case 6: 74 case 9: 75 case 11: 76 chaDays += 30; 77 break; 78 case 2: 79 // 如果 年能被4整除 但不能被100 整除 + 年被400整除 80 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 81 chaDays += 29; 82 } else { 83 chaDays += 28; 84 } 85 break; 86 } 87 } 88 89 90 // System.out.println("差的天数为:" + chaDays); 91 92 int weekOfDay = (chaDays +1) % 7;//加一的原因是周日为数组中的第一个 93 // System.out.println(weekOfDay); 94 for (int i = 0; i < weekOfDay; i++) { 95 System.out.print("\t");//空格补位 96 } 97 for (int i = 1; i <= days; i++) { 98 System.out.print(i + "\t"); 99 if ((i + weekOfDay) % 7 == 0) {//每一行 七个就换行 100 System.out.println(); 101 } 102 } 103 104 } 105 public static void main(String[] args) { 106 107 WanNianLi wNianLi=new WanNianLi(); 108 wNianLi.menu(); 109 110 111 } 112 }
时间: 2024-10-28 21:08:22