Java Udemy课程闰年计算器-无法找出我做错了什么

46scxncf  于 2023-03-28  发布在  Java
关注(0)|答案(3)|浏览(126)

我无法找出为什么我的代码不工作。我已经想出了另一种方法来解决这个问题,但我想知道为什么它不会像我一样工作。这就是我想知道的。我不需要解决它。
编写一个isLeapYear方法,参数类型为int,名为year。
该参数需要大于或等于1且小于或等于9999。
如果参数不在该范围内,则返回false。
否则,如果在有效范围内,则计算年份是否为闰年,如果是闰年则返回true,否则返回false。
要确定某年是否为闰年,请执行以下步骤:
1.如果年份可被4整除,请转到步骤2。否则,请转到步骤5。
1.如果年份可被100整除,则转到步骤3。否则,转到步骤4。
1.如果年份可被400整除,请转到步骤4。否则,请转到步骤5。
1.这一年是闰年(它有366天)。方法isLeapYear需要返回true。
1.年份不是闰年(它有365天)。方法isLeapYear需要返回false。
以下年份不是闰年:1700,1800,1900,2100,2200,2300,2500,2600这是因为它们能被100整除,但不能被400整除。
以下年份为闰年:1600,2000,2400这是因为它们可以被100和400整除。
输入/输出示例:
isLeapYear(-1600);→应该返回false,因为参数不在范围(1-9999)内
是闰年(1600年);→应该返回true,因为1600年是闰年
isLeapYear(2017);→应返回false,因为2017年不是闰年
isLeapYear(2000);→应返回true,因为2000年是闰年
注意:isLeapYear方法需要被定义为public static,就像我们在课程中所做的那样。不要将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;
        }
    }
}
xesrikrc

xesrikrc1#

我复制粘贴了你的指令/你的代码:
1.如果年份可被4整除,请转到步骤2。否则,请转到步骤5。
1.如果年份可被100整除,则转到步骤3。否则,转到步骤4。
1.如果年份可被400整除,请转到步骤4。否则,请转到步骤5。
1.这一年是闰年(它有366天)。方法isLeapYear需要返回true。
1.年份不是闰年(它有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整除”等效于“不是y % 4!= 0”
  • 等等。

希望能帮上忙

dbf7pr2w

dbf7pr2w2#

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

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
   }
}
k75qkfdt

k75qkfdt3#

我注意到的第一件事是

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

应为:

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

除此之外,代码看起来很好,尽管还可以改进。
你能给予个例子说明你得到的输出和你想要的输出有什么不同吗?

相关问题