寻找下一个leap年Java

问题描述

代码在第一年是a年时起作用,因此,如果我说年份是2004年,它将返回2008年,但是当起始年不是a年时,它将不返回任何内容。例如,如果给定的年份是2001年,2002年或2003年,那么下一个leap年将是2004年,我将如何输出。我知道while循环是有意义的,但我不知道在其中放入什么。另外,我只能使用基本的Java格式,因此没有输入的类,如java.time.Year

public static boolean leapYear(int year) {
  boolean isLeap = true;
  if (year % 4 == 0) {
    if (year % 100 == 0) {
      if (year % 400 == 0)
        isLeap = true;
      else
        isLeap = false;
    } else
      isLeap = true;
  } else {
    isLeap = false;
  }
  return isLeap;
}

public static int nextLeapYear(int year) {
  int nextLeap = 0;

  if (leapYear(year)) {
    nextLeap = nextLeap + 4;
  } else {
    while (!leapYear(year)) {
      nextLeap++;
    }
    nextLeap += 1;
  }
  year += nextLeap;
  return year;
}

解决方法

当然,这不需要太多处理。但是,如果您想提高程序效率,可以考虑以下几点:

  • 所有leap年必须都可以被4整除。但是并非所有被4整除的年都是years年。因此,首先检查是否被4整除。这将是75%的情况。在这种情况下,返回false
   if (year % 4 != 0) {
        return false;
   }
  • 继续操作year must be divisible by 4,请确保它不是一个世纪年。将在25%的时间内评估此结果,并在24%的时间内返回true。
   if (year % 100 != 0) {
       return true;
   }
  • 最后,唯一未检查的类别是年份除以400。如果按照逻辑上的说明,则必须是一个世纪年份。因此相应地返回。这将算得上是.25%的时间。
  return year % 400 == 0;

,

您的代码有多种破损方式:

  1. 您说:如果给定的年份是a年,则下一个leap年将是4年后。 这是错误的。如果您在1896年通过,则您的算法将返回1900,但这是错误的。 1896年是a年,而1900年不是not年。

  2. 如果您提前退出,则isLeapYear代码将更容易阅读。该方法应该有很多return语句,并且缩进较少。

  3. 您的while循环将不断重复询问相同的问题,并且如果您对稳定的方法提出相同的问题(并且isLeapYear方法是稳定的),则会得到相同的答案,从而导致无限循环。大概,您不需要while(!leapYear(year)),不需要while(!leapYear(year + nextLeap)),也不想在while循环后再次增加nextLeap。

  4. 实际上,您的特殊情况是:如果指定的年份已经是一年,则根本不需要加4-。考虑一下:您可以消除if / else部分。您的代码将仅为nextLeap,while循环和return语句。如果您做对的话,则是3排。

,

编辑:我知道了!

对于任何为此苦苦挣扎的人来说,这就是对我有用的方法:)

public static boolean leapYear(int year) {
    if(year % 4 == 0)
    {
        if( year % 100 == 0)
        {
            // year is divisible by 400,hence the year is a leap year
            if ( year % 400 == 0)
                return true;
            else
                return false;
        }
        else
            return true;
    }
    else
        return false;
    }



public static int nextLeapYear (int year) {
    int nextLeap = 0;
         while(leapYear(year + nextLeap) == false){
             nextLeap++;
        
         }
        
        if(leapYear(year) == true)
            nextLeap += 4;
        
        year += nextLeap;
        return year;

    }
,

您可以大大简化功能leapYear,如下所示:

public static boolean leapYear(int year) {
  return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}

演示:

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(leapYear(1999));
        System.out.println(leapYear(2000));
        System.out.println(leapYear(1900));
        System.out.println(leapYear(1904));
    }

    public static boolean leapYear(int year) {
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
}

输出:

false
true
false
true

然后,您可以在nextLeapYear函数中使用它,如下所示:

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(nextLeapYear(1999));
        System.out.println(nextLeapYear(2000));
        System.out.println(nextLeapYear(2001));
    }

    public static int nextLeapYear(int year) {
        // If year is already a leap year,return year + 4
        if (leapYear(year)) {
            return year + 4;
        }

        // Otherwise,keep incrementing year by one until you find a leap year
        while (!leapYear(year)) {
            year++;
        }

        return year;
    }

    public static boolean leapYear(int year) {
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
}

输出:

2000
2004
2004

在生产代码中,应该使用OOTB(即用型)类java.time.Year处理一年。

import java.time.Year;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(nextLeapYear(1999));
        System.out.println(nextLeapYear(2000));
        System.out.println(nextLeapYear(2001));
    }

    public static int nextLeapYear(int year) {
        Year yr = Year.of(year);
        // If year is already a leap year,return year + 4
        if (yr.isLeap()) {
            return yr.plusYears(4).getValue();
        }

        // Otherwise,keep incrementing year by one until you find a leap year
        while (!yr.isLeap()) {
            yr = yr.plusYears(1);
        }

        return yr.getValue();
    }
}

输出:

2000
2004
2004

通过 Trail: Date Time 了解有关现代日期时间API的更多信息。

,

要获得nextLeapYear() 100%正确的答案似乎并不容易。请允许我提出一种更简单的思考方式,也可以使我更轻松地正确编写代码和/或修复可能存在的任何错误。

声明一个变量candidateLeapYear来保存一个我们还不知道在year之后的leap年的年份。将其初始化为year + 1,因为我们知道下一个leap年必须严格大于year。在循环测试中,candidateLeapYear是否为a年(使用另一种方法),只要不是leap年,则递增1。循环结束时,candidateLeapYear保持下一个leap年。退货。

快乐的编码。