Here is my requirements. Determine If a Year Is a Leap Year Algorithm:
If the year can be evenly divided by 4, then it is a leap year
Except when the year can be evenly divided by 100, then it is not a leap year
Except when the year can be evenly divided by 400, then it is a leap year
Otherwise, it is not a leap year
I need to know if i did right?
private static boolean isLeapYear(int userInput){
boolean leapYear= false;
if (userInput % 4 == 0 ){
leapYear = true;
if (userInput % 4 == 0 && userInput % 100 ==0) {
leapYear = false;
if(userInput % 400 == 0){
leapYear = true;
}
}
}
else {
leapYear = false;
}
return leapYear;
}
解决方案
userInput % 4 == 0 && userInput % 100 ==0 is equivalent to userInput % 400 == 0
and userInput % 4 == 0 then it is definitely Leap year so need not to check any other condition.