Determine leap year using Java?

回眸只為那壹抹淺笑 提交于 2020-01-16 06:59:11

问题


Here is my requirements. Determine If a Year Is a Leap Year Algorithm:

  • If the year can be evenly divided by 4, then it is a leap year
    • Except when the year can be evenly divided by 100, then it is not a leap year
  • Except when the year can be evenly divided by 400, then it is a leap year
  • Otherwise, it is not a leap year

I need to know if i did right?

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

回答1:


userInput % 4 == 0 && userInput % 100 ==0 is equivalent to userInput % 400 == 0

and userInput % 4 == 0 then it is definitely Leap year so need not to check any other condition.




回答2:


I used this one in C++.

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




回答3:


Better use this condition for a valid leap year
(((year%4 == 0) && (year%100 !=0)) || (year%400==0))

Here is a similar C program to check leap year.




回答4:


Correct! Simplified:

  1. Remove year % 4 from 2nd if bc already tested in 1st if
  2. Remove else bc already set leap = False at top

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-Line:

def is_leap(year):
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0



回答5:


I used this short method:

private static Boolean isLeapYear(int year) {
        return year % 4 == 0 ? (year % 100 == 0 ? ( year % 400 == 0 ? true : false) : true) : false ;
    }



回答6:


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)



回答7:


From 1700 to 1917, official calendar was the Julian calendar. Since then they we use the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918, when the next day after January 31st was February 14th. This means that 32nd day in 1918, was the February 14th.

In both calendar systems, February is the only month with a variable amount of days, it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4 while in the Gregorian calendar, leap years are either of the following:

Divisible by 400.

Divisible by 4 and not divisible by 100.

So the program for leap year will be:

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


来源:https://stackoverflow.com/questions/12967390/determine-leap-year-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!