0

这是我的要求。确定年份是否为闰年算法:

  • 如果年份可以被4整除,则为闰年
    • 除非年份可以被100整除,否则就不是闰年
  • 除非年份可以被400整除,否则就是闰年
  • 否则不是闰年

我需要知道我做对了吗?

private static boolean isLeapYear(int userInput){
    boolean leapYear= false;

    if (userInput % 4 == 0 ){
        leapYear = true;

        if (userInput % 4 == 0 && userInput % 100 ==0) {
            leapYear = false;

            if(userInput % 400 == 0){
                leapYear = true;
            }
        }
    }
    else {
        leapYear = false;
    }

    return leapYear;
}
4

9 回答 9

0

我在 C++ 中使用了这个。

return ((userInput % 400) || ((userInput % 4) && !(userInput % 100)));

于 2015-03-25T20:48:06.057 回答
0

最好将此条件用于有效的闰年
(((year%4 == 0) && (year%100 !=0)) || (year%400==0))

这是一个类似的C 程序来检查闰年

于 2015-05-24T15:08:03.983 回答
0

正确的!简化:

  1. 从已经在第 1 次测试的第year % 42 次ifbc 中删除if
  2. 删除已设置在顶部的elsebcleap = False

Python:

def is_leap(year):
    leap = False

    if year % 4 == 0:
        leap = True

        if year % 100 == 0:
            leap = False

            if year % 400 == 0:
                leap = True

    return leap

1 行:

def is_leap(year):
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
于 2016-08-15T17:40:52.617 回答
0

我使用了这个简短的方法:

private static Boolean isLeapYear(int year) {
        return year % 4 == 0 ? (year % 100 == 0 ? ( year % 400 == 0 ? true : false) : true) : false ;
    }
于 2017-05-29T19:55:34.833 回答
0
year = int(input("Enter year to determine if it is a leap year"))

def leap_year(year):
    """This is a function to determine if a year
       is a leap year"""
    if year%4==0 and year%100!=0:
        print("This is a Leap year")
        if year%400==0:
            print ("This is a Leap year")
    else:
        print ("This is not a leap year")    


leap_year(year)
于 2019-04-05T07:25:47.583 回答
0

从 1700 年到 1917 年,官方日历是儒略历。从那以后他们我们使用公历系统。从儒略历到公历系统的过渡发生在 1918 年,当时 1 月 31 日之后的第二天是 2 月 14 日。这意味着 1918 年的第 32 天,是 2 月 14 日。

在这两个日历系统中,二月是唯一一个天数可变的月份,闰年有 29 天,其他年份有 28 天。在儒略历中,闰年可以被 4 整除,而在公历中,闰年是以下之一:

能被 400 整除。

能被 4 整除,不能被 100 整除。

所以闰年的程序是:

def leap_notleap(year):

    yr = ''
    if year <= 1917:
        if year % 4 == 0:
            yr = 'leap'
        else:
            yr = 'not leap'
    elif year >= 1919:
        if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
            yr = 'leap'
        else:
            yr = 'not leap'
    else:
        yr = 'none actually, since feb had only 14 days'

    return yr
于 2019-10-01T15:19:01.853 回答
0

使用python和其他语言,您可以使用如果您减去 1 天来游行,您将拥有 2 月的最后一天的属性。如果是闰年,那一天是 29 日。

from datetime import datetime, timedelta

def is_leap_year(year: int):
    marzo = datetime(year, 3, 1)
    febrero = marzo - timedelta(1)
    if febrero.day == 29:
        return True
    else:
        return False
于 2021-03-18T19:54:54.837 回答
0

能被 4 整除的每一年都是闰年,但能被 100 整除的年份除外,但如果这些百年年份能被 400 整除,则它们就是闰年。例如,1700 年、1800 年和 1900 年是不是闰年,而是 1600 年和 2000 年。 [2]

— 美国海军天文台

资料来源:https ://en.wikipedia.org/wiki/Gregorian_calendar

因此,对于有资格成为闰年的年份,它应该是:

  1. 可以被 400 整除 - 或 -
  2. 能被 4 整除但不能被 100 整除

在您的代码中使用此事实,如下所示:

if((userInput % 400 == 0) || (userInput % 4 == 0 && userInput % 100 != 0)) {
     System.out.println(userInput + " is a leap year");
}

对于生产代码,我建议您使用 OOTB java.time API:

if (java.time.Year.isLeap(userInput)) {
    System.out.println(userInput + " is a leap year");
}

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

于 2021-03-18T20:11:28.287 回答
-1

userInput % 4 == 0 && userInput % 100 ==0相当于userInput % 400 == 0

然后userInput % 4 == 0肯定是闰年,所以不需要检查任何其他条件。

于 2012-10-19T04:32:33.803 回答