简体   繁体   中英

For Loop: Leap year

I'm trying to write a program that displays ten leap years per line until the year 2100.

The code I have so far is this:

public class leapYear {
public static void main (String args[]){
    int leapYear = 2001; // initialize leapYear variable to 2001

    if (leapYear <= 2100){
        for (int x = 0; x <= 10; x++){ // print ten leap years per line
            System.out.print(leapYear + " ");
            leapYear = leapYear + 4;
        }// end of for loop
        System.out.println(" ");
        int x = 0;
    } // end of if statement

}// end of main method

}// end of class

The output I get from the code is:

2001 2005 2009 2013 2017 2021 2025 2029 2033 2037 2041  

I'm having trouble finding a condition that will allow me to print the next series of leapYears on the following lines. Any guidance would be appreciate.

Change the if(leapYear <= 2100) line to while(leapYear <= 2100) .

The if statement will only execute once, meaning you print out 10 dates. A while loop will execute as many times as it takes for leapYear to be greater than 2100.

You are currently using an if statement to check whether or not you want to print something ( if (leapYear <= 2100) ). However, after your for-loop you want to check again whether there are still more leap years to print before reaching the year 2100 . This can be accomplished by using a while loop instead, ie while(leapYear < 2100) . (As 2100 is not a leap year, see below)

Additionally, it should be noted that 2004 is a better starting place for leap-years and 2100 is not a leap year (wheareas 2000 was). See the wiki for more information.

Second note unrelated to your problem, you can also use System.out.println(); (so without an argument) to print the newline you are after.

Third and final, the line int x = 0; is not necessary as the counter x in your for loop no longer exists outside of that loop.

Your If statement is whats giving you the trouble, it only executes once. You need something like a while loop, or you could use a for loop like so,

                for (int leapYear = 2001; leapYear <= 2100; leapYear += 4) {
                    if((leapYear - 1) % 40 == 0) {
                        System.out.println(" ");
                    }
                    System.out.print(leapYear + " ");
                }

However I would like to point out that none of the years you print out are leap years. leap years are those divisible by 4 except when divisible by 100 except when also divisible by 400 (ie 2000 was a leap year 2004 was and 2100 won't be but 2400 will be) super confusing you may want to check this out on leap years.

Good luck!

You asked how to have 10 leap years print per line. To accomplish this, simply change:
if (leapYear <= 2100) {
To:
while (leapYear <= 2100) {

With the full working and tested example here:

public class LeapYear {
    public static void main(String[] args) {

        int leapYear = 2001; // initialize leapYear variable to 2001

        while (leapYear <= 2100) { // a simple change of "If" to "While" here
                                    // will do the trick
            for (int x = 0; x < 10; x++) {
                System.out.print(leapYear + " "); // print ten leap years per
                                                    // line
                leapYear = leapYear + 4;
            }
            System.out.println();
        } // end of while loop
    } // end of main method
}// end of class

That being said, I would recommend some of the things that have already been mentioned.

1 - There are some logic errors in your code that will incorrectly name some years leap years. See here for more information on Leap Years.

2 - If you try my example, you'll also notice that the leap years are shown past 2100. You'll need to figure out a way to correctly structure your code if you don't want the numbers to be displayed past 2100.

3 - I would strongly recommend you take a look at the GregorianCalendar class, as it will simplify your code to check for a leap year. However, if you are doing this to learn or for a school assignment, I would recommend looking at the link under number 1.

4 - More of an aside, but in your code you have:

leapYear = leapYear + 4

This can be simplified to:

leapYear += 4

Hopefully this helps clarify things for you.

Providing the correct implementation for leap year:

public void main(String[] args) {
    for(int year = 2001 ; year <= 2100 ; ++year) {
        if(isLeapYear(year)) {
            System.out.println(year);
        }
    }
}

public static boolean isLeapYear(int year) {
    if (year % 4 != 0) {
        return false;
    } else if (year % 400 == 0) {
        return true;
    } else if (year % 100 == 0) {
        return false;
    } else {
        return true;
    }
}
public static void main (String args[]){
    int leapYear = 2001; // initialize leapYear variable to 2001

    while (leapYear <= 2100){
        for (int x = 0; x < 10; x++){ // print ten leap years per line
            if (leapYear > 2100){ // if leapYear is over 2100, stop printing
                break;
            }
            System.out.print(leapYear + " ");
            leapYear = leapYear + 4;
        }// end of for loop
        System.out.println(" ");
        int x = 0;
    } // end of if statement

}// end of main method

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