3

我正在编写一个必须满足前提条件的代码,如果条件都满足,那么它将返回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;
}
4

6 回答 6

5

当检查失败时返回 false 。如果先决条件之一失败,则无需进一步检查。

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) return false;
    if(mon>12) return false;

    //checks to see if the years are greater than 1
    if(year<=0) return false;

    //checks to see if the days are between 1 and 31
    if(day<=0) return false;
    if(day>31) return false;

    //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;
}
于 2017-02-23T18:32:49.207 回答
1

在代码顶部添加一个布尔变量:

bool legit = true;

在每个if语句中,如果条件为 false,则更改 legit 的值。如果值为 true,请不要更改它。

在条件结束时,返回变量:

return legit;

如果任何检查不合法,该方法将返回 false。

编辑:Espen 的解决方案更有效(如果稍微不那么准确 - 请参阅评论),尽管我OR排除了双重条款:

 if((mon < 1) || (mon>12)) return false;

if((day < 1) || (day > 31)) return false; 

但是请注意,这仍然可以将无效日期返回为有效日期,例如:6 月 31 日

于 2017-02-23T18:31:14.627 回答
0

只是另一个建议。

对于这种特定情况,它可能看起来不太好,但可以帮助有类似问题的人(“要求多个条件返回 true”)。

public static boolean isLegitimate(int mon, int day, int year){
    return Stream.of(
            checkMonth(mon),
            checkYear(year),
            checkDay(day),
            checkFebruary(mon, day, year))
    .allMatch(check -> check);
}
于 2019-03-11T22:10:54.243 回答
0

您可以使用JodaTime API

 public static boolean isLegitmate(int mon, int day, int year){
    try {
        new DateTime().withMonthOfYear(mon).withYear(year).withDayOfMonth(day);
        return true;
    } catch (Exception e){
        return false;
    }
}
于 2017-02-23T18:29:10.353 回答
0

尝试这个 :

public static boolean isLegitimate(int mon, int day, int year){

if(  (mon >= 1 && mon <= 12) && (year > 0) && (day >=0 && day <=31)){
   if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0))
    if (day >29)
        return false;
    return true;
}
}
于 2017-02-23T18:29:18.650 回答
0

这将您的逻辑分解为多种方法。我稍微修改了闰年的逻辑。

public class MultipleConditions {

    public static boolean isLegitimate(int mon, int day, int year) {


        return validMonth(mon) && validYear(year) && validDay(day) && validFebDay(mon, day, year);
    }

    private static boolean validFebDay(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.
        if (mon!=2)
            return true; // Not in feb
        if (year%4 != 0)
            return day <= 28; // Not a leap year

        if (year%100 == 0) {
            return day <= 29; // Divisible by 4, but not a centurial year
        }
        // Divisible by 4, centurial year, and divisible by 400
        if (year%400==0) {
            return day <=29;
        }
        // Divisible by 4, centurial year not divisible by 400
        return day <= 28;
    }

    private static boolean validDay(int day) {
        // checks to see if the days are between 1 and 31
        return day >= 0 && day <= 31;
    }

    private static boolean validYear(int year) {
        return year > 0;
    }

    private static boolean validMonth(int mon) {
        // checks to see if the months are between 1 and 12
        return (mon >= 1) && (mon <= 12);
    }
    public static void main(String args []) {
        System.out.println(isLegitimate(2, 23, 2017));
        System.out.println(isLegitimate(2,29,2017));
        System.out.println(isLegitimate(3,31,2017));
    }
}
于 2017-02-23T18:51:39.583 回答