In advance, I was only a novice in the computer field, and I began to learn Java recently. I later received a programming assignment for calculating the total number of February 29 during the two-day period, and I wrote the procedure in haste. Since there was no programming experience, Java was only learning a small part of it. So at present, the program has not resolved the input problem.
My idea of solving this problem is:
- Write a method (Leapyear) to determine whether a year is a leap years;
- Writing another method (Dateexist) is used to determine whether a period is real or not, and in this method the previous method is referenced;
- Finally, the main method is written, and the reference method (Dateexist) determines whether the starting date and the ending date are real;
- If they are true. If the start and end dates are not in the same year, the year enumeration between the start and end years is calculated for the total number of leap years, in which case the method (Leapyear) is referenced and the hint is displayed if it is not true.
- The final judgment is whether the start and end year are leap years and whether the start/end month is less than/greater than February, and the qualifying February 29 is calculated, and the method Leapyear must be quoted in this process.
Here's My Code:
1 Public classleapyearcomputing2 {3 //Determines whether a year is a leap years. 4 Public Static BooleanLeapyear (inty) {5 Booleanleapyear;6 if(y% 4 = = 0) {7 if(y% 100 = = 0) {8 if(y% 400 = = 0) {9Leapyear =true;Ten } One Else { ALeapyear =false; - }; - } the Else { -Leapyear =true; - }; - } + Else { -Leapyear =false; + }; A returnleapyear; at }; - - //determine whether a date exists, regardless of the year BC. - Public Static BooleanDateexist (intYear,intMonth,intdate) { - Booleandateexist; - if(Year > 0 ) { in if(Month > 0 && Month < 13) { - if(Date > 0 && Date < 32) { to Switch(month) { + Case2: - Case4: the Case6: * Case9: $ Case11:Panax Notoginseng if(Date < 31) { - if(Month = = 2) { the if(Leapyear (year)) { + if(Date < 30) { ADateexist =true; the } + Else { -Dateexist =false; $ }; $ } - Else if(Date < 29) { -Dateexist =true; the } - Else {WuyiDateexist =false; the }; - } Wu Else if(Date < 31) { -Dateexist =true; About } $ Else { -Dateexist =false; - }; - } A Else { +Dateexist =false; the }; - Break; $ default: the if(Date < 32) { theDateexist =true; the } the Else { -Dateexist =false; in }; the }; the } About Else { theDateexist =false; the }; the } + Else { -Dateexist =false; the };Bayi } the Else { theDateexist =false; - }; - returndateexist; the }; the the //calculates the number of February 29 during the two-day period. the Public Static voidMain (string[] args) { - intyear1 = 2013;//enter the starting year. the intMonth1 = 9;//Enter the starting month. the intDate1 = 28;//Enter a start date. the intYEAR2 = 2015;//Enter the end year. 94 intMonth2 = 1;//Enter the month of termination. the intDate2 = 3;//Enter the end date. the intresult = 0; the intYear = year1 + 1;98 if(Dateexist (year1,month1,date1)) { About if(Dateexist (Year2,month2,date2)) { - while(Year <year2) {101 if(Leapyear (year)) {102Result + +;103 };104Year + +; the };106 if(Year1 = =year2) {107Result--;108 };109 if(Month1 <= 2) { the if(Leapyear (year1)) {111Result + +; the };113 }; the if(Month2 > 2) { the if(Leapyear (year2)) { theResult + +;117 };118 };119System.out.println ("Two dates with" + result + "February 29"); - }121 Else {122SYSTEM.OUT.PRINTLN ("Termination date is not real.") ");123 };124 } the Else {126System.out.println ("The start date is not real.") ");127 }; - 129 the 131 the };133}
PS: This procedure does not consider the BC year. I originally considered the situation in BC, but there is no A.D. BC, and the definition of a leap year is most likely not applicable, then the years are limited to A.D.
The number of leap years should be reduced by one when the start and end years are in the same year, and the year is a leap years.
If you find the shortcomings of the program or have a better algorithm, but also hope to advise.
Java program for calculating the total number of February 29 during the two-day period