Two consecutive articles starting with this article will introduce how to use Java basic knowledge to create the 10 thousand keystore console program.
Let's take a look.
Let's analyze the calendar business:
1. the user enters the information --> determines whether the information meets the requirements)
2. calculate the total number of days from January 1, January 1, 1900 to the current day based on Monday January 1, 1900.
(1) calculate the total number of days from January 1, 1900 to (the year entered by the user-1) --> pay attention to the calculation of the point of the year
(2) calculate the number of days from January 1, January (month-1) of the year entered by the user.
3. Calculate the day of the week on the first day of the input month.
4. format the output
Follow these steps to parse the code
1. Use the do-while loop to receive user input information and use the IF-else statement for judgment.
Code:
- Int year;
- Int month;
- Boolean xn = false;
- Do
- {
- System. Out. println ("Enter the year :");
- Year = input. nextint ();
- System. Out. println ("Enter the month :");
- Month = input. nextint ();
- // Use a Boolean expression to judge input information
- Xn = (month <1) | (month> 12) | (year <1 );
- If (Xn)
- {
- System. Out. println ("incorrect input information. Please try again! ");
- }
- } While (Xn );
2. Determine the Ping-leap year and calculate the total number of days from January 1, 1900 to (user input year-1)
Code:
- Int everyyearday = 0; // number of days per year
- Int totalyearsdays = 0; // calculates the number of days in a year.
- Int inputyearday = 0 // records the number of days of the user input year
- Boolean YN = false; // identifies a peat year.
- // Use the for loop to calculate the number of days
- For (INT I = 1900; I <= year; I ++)
- {
- If (I % 4 = 0) & (I % 100! = 0) | (I % 400 = 0) // condition for determining a leap year
- {
- YN = true;
- Every yyearday = 366;
- }
- Else
- {
- YN = false;
- Every yyearday = 365;
- }
- // If the year in the cycle is less than the year entered by the user, the cumulative number of days
- If (I <year)
- {
- Totalyearsdays = totalyearsdays + everyyearday;
- }
- Else
- {
- Inputyearday = everyyearday;
- System. Out. println (Year + "years total:" + inputyearday + "day ");
- }
- }
3. determine the number of days of the month and calculate the number of days from January 1, January to (month-1) of the current year.
Code:
- Int everymonthday = 0; // records the number of days per month
- Int totalmonthsdays = 0; // Total Number of days
- Int inputmonthday = 0; // records the number of days of the month input by the user in the year input by the user
- // Use the for loop to calculate the number of days
- For (INT I = 1; I <= month; I ++)
- {
- Switch (I)
- {
- Case 4:
- Case 6:
- Case 9:
- Case 11:
- Everymonthday = 30;
- Break;
- Case 2:
- If (Xn) // XN is a Boolean variable used to record the year of a leap.
- {
- Everymonthday = 29;
- }
- Else
- {
- Everymonthday = 28;
- }
- Break;
- Default:
- Everymonthday = 31;
- Break;
- }
- If (I <month)
- {
- Totalmonthsdays = totalmonthsdays + everymonthday;
- }
- Else
- {
- Inputmonthday = everymonthday;
- System. Out. println (month + "total months:" + inputmonthday + "day ");
- }
- }
4. calculate the total number of days, and calculate the day of the first day of the week of the month entered by the user.
Code:
- Int Total = totalmonthsdays + totalyearsdays; // calculates the total number of days.
- Int temp = (total + 1) % 7; // determines the day of the week on the first day of the input month.
5. format the output
Code:
- // Because our input format is
- // Sunday, Monday, Wednesday, Friday, Saturday
- // When it is Sunday, we can output it directly, but when
- // When the first day is Monday, we must print a space first
- // Then output the date to match the number with the day of the week.
- // Print space
- For (INT spaceno = 0; spaceno <temp; spaceno ++)
- {
- System. Out. Print ("/t ");
- }
- // Print numbers in order
- For (INT I = 1; I <= inputmonthday; I ++)
- {
- If (total + I) % 7) // determines if the line feed is correct.
- {
- System. Out. println (I );
- }
- Else
- {
- System. Out. Print (I + "/t ");
- }
- }