提问者:小点点

JavaUdemy课程闰年计算器-不知道我做错了什么


我无法找出为什么我的代码不起作用。我已经想出了解决这个问题的另一种方法,但我想知道为什么它不会像我一样工作。这就是我想了解的。我不需要它被解决。

Leap年份计算器编写一个方法isLeap年份,其参数为int类型,名为年份。

该参数需要大于或等于1且小于或等于9999。

如果参数不在该范围内,则返回 false。

否则,如果它在有效范围内,则计算年份是否为闰年,如果是闰年则返回 true,否则返回 false。

若要确定年份是否为闰年,请执行以下步骤:

    < li >如果年份能被4整除,请转到步骤2。否则,转到步骤5。 < li >如果年份能被100整除,请转到步骤3。否则,转到步骤4。 < li >如果年份能被400整除,请转到步骤4。否则,转到步骤5。 <李>今年是闰年(有366天)。方法isLeapYear需要返回true。 < li >这一年不是闰年(它有365天)。方法isLeapYear需要返回false。

以下年份不是闰年:1700、1800、1900、2100、2200、2300、2500、2600这是因为它们可以被100整除,但不能被400整除。

接下来的年份是闰年:1600,2000,2400这是因为它们可以被100和400均匀地整除。

输入/输出示例:

isLeapYear(-1600);→ 应返回false,因为参数不在范围(1-999)内

isLeapYear(1600);→应该返回true,因为1600年是闰年

是闰年(2017);→应该返回 false,因为 2017 年不是闰年

isLeapYear(2000年);→应该返回true,因为2000年是闰年

注意:方法是LeapYear需要定义为公共静态,就像我们到目前为止在课程中所做的那样。注意:不要将 main 方法添加到解决方案代码。





My Code:
public class LeapYear {

    public static boolean isLeapYear (int year) {

        //Out of range
        if ( year >= 1 && year <= 9999) {
            return false;
        }
        //calculating if the year is a leap year or not
        if ( year % 4 == 0 ) {
            if ( year % 100 == 0) {
                if ( year % 400 == 0) {
                    return true;
                }else {
                    return false;
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}

共3个答案

匿名用户

我复制粘贴了您的说明/代码:

>

  • 如果年份能被 4 整除,请转到步骤 2。否则,请转到步骤 5。

    如果年份可被 100 整除,请转到步骤 3。否则,请转到步骤 4。

    如果年份可以被400整除,请转到步骤4。否则,转至步骤5。

    这一年是闰年(有366天)。方法isLeapYear需要返回true。

    这一年不是闰年(它有365天)。方法isLeapYear需要返回false。

     public class LeapYear {
    
       public static boolean isLeapYear (int year) {
         //0) greater than or equal to 1 and less than or equal to 9999.
         if (year < 1 || year > 9999) {
           return false;
         }
    
         //1) If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5
         if (year % 4 != 0 ) {
           return false;
         }
    
         //2) If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
         if (year % 100 != 0) {
           return true;
         }
    
         //3) If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5
         if (year % 400 != 0) {
           return false;
         }
    
         //4) The year is a leap year (it has 366 days)
         return true;
       }
     }
    

    通常,简化表达式的“技巧”是实现以下内容:

    • "x大于等于1"相当于"x不小于1"
    • "y可被4整除"等价于"not y%4!=0"
    • 等等。

    “希望这有帮助……”。。。

  • 匿名用户

    如果你稍微重新安排一下你的步骤,你可以用更少的步骤来完成,如下。

    public class LeapYear {
    
       public static boolean isLeapYear (int year) {
           // check range - only years between 1 and 9999 inclusive allowed
          if ( year < 1 || year > 9999) {
                return false;
           }
    
          // If the year is evenly divisible by 400 return true.
          if (year % 400 == 0) {
              return true;
          }
    
          // If the year is evenly divisible 100 return false
          if (year % 100 == 0) {
             return false;
          }
    
          // if year is divisible by 4 return true/ else false.
          
          return year % 4 == 0; //returns true or false depending on year
       }
    }
    
    

    匿名用户

    我能注意到的第一件事是:

    //Out of range
    if ( year >= 1 && year <= 9999) {
       return false;
    }
    

    灵魂是:

    //Out of range
    if ( year < 1 || year > 9999) {
       return false;
    }
    

    因为它写错了。

    除此之外,代码似乎很好,尽管可以改进。

    能否举例说明您获得的输出与您想要的输出有何不同?

    相关问题