Question: Enter year and month, and print the calendar for the corresponding year and month.
Example:
Problem analysis:
1. First of all, 1970 was the birth of the Unix system. It became the first year of Unix in 1970. January 1, 1970 was Thursday. Now most mobile phones can only display the calendar functions until January 1, 1970;
2. If you want to print a calendar for a certain year and month, you should first calculate what day of the week it is on the 1st of the month?
Resolve what day of the week it is?
2.1: First calculate the number of days in the year, that is, the number of days as of January 1 of this year, use the for loop, starting from 1970, with a leap year +366 and a normal year +365;
2.2: Calculate the number of month days, that is, the number of days as of the 1st of this month, use the for loop, and calculate the number of month days starting from January;
2.1.1: Use leap year judgment and use switch to judge the number of return months and days;
2.3: Use the number of days in the year plus the number of days in the month to find the total number of days from January 1, 1970, and use the total number of days to determine the day of the week on the 1st of this month;
3. Determine the total number of days this month (using the switch statement);
4. Print the calendar;
Code implementation:
A function implements printing PrintTitle(); that is, implements a fixed header part;
Implement Print (parameter 1, parameter 2, parameter 3); that is, print the calendar theme part;
Parameter 1 (calculate the total number of days): calculate the number of days in year and calculate the number of days in month;
Parameter 2 passes the year;
Parameter 3 passes the month;
To determine the leap year setting independent functions;
Summarize:
Before solving problems, you must first analyze the problem, gradually divide the big problem into small problems, clarify the logic, and realize the logic of small problems, and the entire problem has been achieved by 80%;
From bottom to top, gradually implement, function calls independently;
Code implementation part:
-
--------------------------------------------------------------------------------------------------------------------------------
----
--------
--------
--------
--------
The above basic Java article simple implementation (case) of printing perpetual calendar is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.