我正在编写一个必须满足前提条件的代码,如果条件都满足,那么它将返回true。我尝试了多个“if”语句,但这似乎不起作用。嵌套 if 语句似乎不是这里的答案,我认为“else if”语句不会起作用。我要问的是,这样做的正确方法是什么?我是否写错了 if 语句?
这是我的代码:
public static boolean isLegitimate(int mon, int day, int year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
// TODO 1: Check if a date is valid.
//checks to see if the months are between 1 and 12
if((mon >= 1) && (mon <= 12)) {
}
//checks to see if the years are greater than 1
if (year > 0){
}
//checks to see if the days are between 1 and 31
if ((day >=0) && (day <=31)){
}
//This checks that if the month is February, is divisible by 4 evenly,
//and is divisible by 100 evenly, then the days can not exceed 29
if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0)){
if (day >29){
return false;
}
}
return true;
}