Perpetual calendar (II), Perpetual Calendar
Prepare Perpetual calendar program based on if and
In the previous article, we used to determine the number of days in the year and month. There are several key points in this article.
① Calculate the total number of days before January 1, January 1, 1900
② Calculate the number of days of the input month (for example, enter 5, the total number of days of the first 4 months)
③ Day of the week: 1 + total days % 7 \ t
④ Input 7 entries per line
Complete Perpetual calendar code:
1 import java. util. seconds; 2 public class Calendars {3 public static void main (String [] argas) 4 {5 seconds input = new seconds (System. in); 6 int year; // year 7 int month; // month 8 int days = 0; // day 9 boolean isRn; // used to determine whether it is a leap year, true is a leap year, and false is a year of 10 int totalDays = 0; 11 System. out. println ("******************* use calendar ***************** *"); 12 System. out. println ("Enter the year"); 13 year = input. nextInt (); 14 System. out. println ("Enter the month"); 15 month = input. nextInt (); 16 // determines whether it is a leap year 17 if (year % 400 = 0) | (year % 4 = 0 & year % 100! = 0) 18 {19 isRn = true; 20 System. out. println (year + ""); 21} 22 else 23 {24 isRn = false; 25 System. out. println (year + "year is normal"); 26} 27 // calculates the total number of days of the year 28 for (int I = 1900; I <year; I ++) 29 {30 if (I % 400 = 0) | (I % 4 = 0 & I % 100! = 0) 31 {32 totalDays + = 366; 33} 34 else 35 {36 totalDays + = 365; 37} 38} 39 // calculate the sum of the days before the input month 40 for (int I = 1; I <= month; I ++) 41 {42 switch (I) 43 {44 case 1: 45 case 3: 46 case 5: 47 case 7: 48 case 8: 49 case 10: 50 case 12: 51 days = 31; 52 break; 53 case 2: 54 if (isRn) 55 {56 days = 29; 57} 58 else 59 {60 days = 28; 61} 62 break; 63 case 4: 64 case 6: 65 case 9: 66 case 11: 67 days = 30; 68 break; 6 9 default: 70 System. out. print ("incorrect input month"); 71 break; 72} 73 // enter the number of days of the month to save in days, but do not accumulate 74 if (I! = Month) 75 {76 totalDays + = days; 77} 78} 79 80 // find the day of the week, which is the number of the previous \ t 81 int beforeDays; 82 beforeDays = 1 + totalDays % 7; 83 if (beforeDays = 7) 84 {85 beforeDays = 0; // representing Sunday, 0 \ t 86} 87 System. out. println ("Sunday \ t Monday \ t Tuesday \ t Wednesday \ t Friday \ t Saturday"); 88 // Print \ t 89 for (int I = 0; I <beforeDays; I ++) 90 {91 System. out. print ("\ t"); 92} 93 94 for (int I = 1; I <= days; I ++) 95 {96 System. out. print (I + "\ t"); 97 // full 7 line breaks 98 if (I + beforeDays) % 7 = 0) 99 {100 System. out. println (); 101} 102} 103 104 // System. out. print (year + "year" + month + "months total" + days + "days"); 105 106} 107}View Code