简体   繁体   中英

Leap year Java Q

I need to write a method for a leap year. So the method has to be called printleap and checks to see if the number is a leap year or not. So the the java program will test the method by reading a list of year number and only displays the leap years. Here is what I have done:

import java.util.Scanner; public class Ex1PartAassig3 {

public static int printleap(String string) {
    Scanner sc1=new Scanner(System.in);
    {
        System.out.println("This programe calculates leap year.");
        int year= printleap ("Enter the year:");
        if ((year%4==0)&& year % 100 !=0)
    }
    System.out.println(year + "is a leap year.");
    {
    else if ((year % 4==0) && (year % 100==0)&&(year % 400==0))
    {
        System.out.println(year +"is a leap year.");
    }
    else {
    }
        System.out.println(year + " is not a leap year.");

    }

}

}

The areas that are showing a mistake are underline the first 'else' else if ((year % 4==0) && (year % 100==0)&&(year % 400==0)

and two curly brackets, the first just above the second system.out and the 1st bracket at the very bottom.There is 3 brackets on the bottom but its the first 1.

Could some help me to run this program or what I have forgotten thank you?

Your curly braces are all over the place and don't line up. This would be a fixed version with properly matched braces:

public static void printleap()
{
    try (Scanner sc1=new Scanner(System.in))
    {
        System.out.println("This programe calculates leap year.");
        System.out.print("Enter the year:");
        int year= sc1.nextInt();
        if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
        {
            System.out.println(year +"is a leap year.");
        }
        else
        {
            System.out.println(year + " is not a leap year.");
        }
    }
}

EDIT: Also updated the if-statement to use the condition of the answer below, since the one in the initial code is indeed flawed. A leap year must be evenly divisible by 4, but a year that is evenly divisible by 100 is only a leap year if it's also evenly divisible by 400.

your condition is wrong...

it must be

 if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))

Your logic (uses if and else if ) should be as follows:

if ( year%400 == 0)
    System.out.println(year+ " is leap year");
  else if ( year%100 == 0)
    System.out.println(year+ " is NOT leap year");
  else if ( year%4 == 0 )
    System.out.println(year+ " is leap year");
  else
     System.out.println(year+ " is NOT leap year");

Using java.time

There's a class for that .

Year.now()
    .isLeap()

Better to always specify your desired/expected time zone rather than rely implicitly on the JVM's current default zone.

Year.now( ZoneId.of( "Europe/Paris" ) )
    .isLeap()

Or specify a year.

Year.of( 2017 )
    .isLeap()

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM