Code:
- ImportJava. Io. bufferedreader;
- ImportJava. Io. ioexception;
- ImportJava. Io. inputstreamreader;
- Public ClassTextcontrol {
- Static IntYear, monthday, weekday; // defines static variables for other classes to call
- Public Static VoidMain (string [] ARGs)ThrowsIoexception
- {
- System. Out. Print ("enter a year :");
- Inputstreamreader IR; // The following six lines of code are accepted from the console.
- Bufferedreader in;
- IR =NewInputstreamreader (system. In );
- In =NewBufferedreader (IR );
- String S = in. Readline ();
- Year = integer. parseint (s );
- Weekday = firstday (year); // calculate the day of the week on the first day of the year.
- System. Out. println ("/N" + year + "year ");
- Printmonth ();
- }
- Public Static BooleanIsleapyear (IntY) // whether the rebellion is a leap year
- {
- Return(Y % 4 = 0 & Y % 100! = 0) | (Y % 400 = 0 ));
- }
- Public Static IntFirstday (IntY) // calculate the day of the week on the first day of the year.
- {
- LongN = y * 365;
- For(IntI = 1; I <Y; I ++)
- If(Isleapyear (I ))
- N + = 1;
- Return(Int) N % 7;
- }
- Public Static VoidPrintweek () // print the header
- {
- System. Out. println ("========================= ");
- System. Out. println ("May 25, 1234 ");
- }
- Public Static IntGetmonthday (IntM) // obtain the number of days per month
- {
- Switch(M)
- {
- Case1:
- Case3:
- Case5:
- Case7:
- Case8:
- Case10:
- Case12:Return31;
- Case4:
- Case6:
- Case9:
- Case11:Return30;
- Case2:
- If(Isleapyear (year ))Return29;
- Else Return28;
- Default:Return0;
- }
- }
- Public Static VoidPrintmonth () // print each month based on different conditions
- {
- For(IntM = 1; m <= 12; m ++) // Loop
- {
- System. Out. println (m + "month ");
- Printweek ();
- For(IntJ = 1; j <= weekday; j ++) // print the corresponding space based on the day of the week on the first day of each month.
- System. Out. Print ("");
- IntMonthday = getmonthday (m); // obtain the number of days per month
- For(IntD = 1; D <= monthday; D ++)
- {
- If(D <10) // format the output in the following four rows
- System. Out. Print (D + "");
- Else
- System. Out. Print (D + "");
- Weekday = (weekday + 1) % 7; // after each day is printed, the Second Day is the day of the week.
- If(Weekday = 0) // if the next day is Sunday, wrap the line.
- System. Out. println ();
- }
- System. Out. println ('/N ');
- }
- }
This program is seen in a book, but the middle method is not clear, the following section:
Code:
- Public static int firstday (INT y) // calculates the day of the week on the first day of the year.
- {
- Long n = y * 365;
- For (INT I = 1; I <Y; I ++)
- If (isleapyear (I ))
- N + = 1;
- Return (INT) n % 7;
- }
Thank you for your advice.