سؤال

public class LeapYear_2 {

    public static void main(String[] args) {

    int year = 1900;                
        while (year <= 2100 && (year % 4 == 0)){

            System.out.println(year + " Is a Leap Year");
            year++;

            System.out.println(year + " Is not a leap year");
            year++;
        }   

    }
}

I just want to know if whats wrong with my Codes? I want to create a program that year 1900 to 2100 will show the leap year and which is NOT.

I just dont know how to use while with many conditions... it seems that I have to have many conditions in while loop in order for this program to work as i want to.

هل كانت مفيدة؟

المحلول

This is what you want to do :

public class TestLeapYear {

public static void main(String[] args) {

    int year = 1900;                
    while (year <= 2100 ){

        if (year % 4 == 0){
            System.out.println(year + " Is a Leap Year");
            year++;
        }
        else {
            System.out.println(year + " Is not a leap year");
            year++;
        }
    }   

}


}

This code goes through all years from 1900 to 2100 and for each of them checks if it is a leap year (year%4==0). Then it prints accordingly.

Edit : you can also do it in one line using the ternary operator (condition ? doIfTrue: doIfFalse) (but it is less readable...)

public static void main(String[] args) {

    int year = 1900;                
    while (year <= 2100 ){
            System.out.println(year + " Is "+ ((year % 4 == 0)? "" : "not")+" a Leap Year");
            year++;

    }   

}

In your original Code :

You are misusing the while loop. The principle of a while loop is to do the same thing until a condition is true.

So this :

 while(condition){ doSomething()}

can be translated into : while the condition is true then I'll doSomething() when it's not true anymore I'll continue.

In your original code the condition is year <= 2100 && (year % 4 == 0) so it is true only if the year is lesser or equal to 2100 AND the year modulo 4 equals 0. And this is this second condition that is false thus exiting the loop.

See how I used an IF ELSE statement inside the loop ? The loop is going through all the years and for each of them we test if it is a leap year of not.

About Leap Years :

Your way of determining if a year is a leap year is not complete. Wikipedia proposes a good algorithm :

 if year is divisible by 400 then
   is_leap_year
 else if year is divisible by 100 then
   not_leap_year
 else if year is divisible by 4 then
   is_leap_year
 else
   not_leap_year

نصائح أخرى

I believe this is what you are looking for:

public static void main(String[] args) {
int year = 1900;                
    while (year <= 2100){
        if (year % 4 == 0) {
            System.out.println(year + " Is a Leap Year");
            year++;
        }
        else {
            System.out.println(year + " Is not a leap year");
            year++;
        }
    }
}

So the while loop takes care of looping through all the years and the if statement checks each year if it matches your leap year query. When you have that in your while loop as you did, whenever you run across a non-leap year it will also break the loop.

Just for fun a cheat code ;)

int count = 0;
for (int i = 1900; i < 2100; i++){
 if (count == 4){
  System.out.println(i + " Is a Leap Year");
  count = 0;
  }
  count++;
}

this single condition will work as you want here I used ternary operator ,it works same as if else condition , Basic syntax is your condition ? true : false

in your case 
    condition : year % 4==0 
    true :  year Is a Leap Year
    false : year Is a not Leap Year


            int year = 1900;                
            while (year <= 2100){
                System.out.println(year % 4==0 ? year+" Is a Leap Year" :year + " Is not a leap year" );
                year++;
            }

I'd like to add that in most presented answers, 1900 will show up as a leap year. That is because there's only a check to see if the year is divisible by 4, but a centurial year should also be divisible by 400. Therefore, 1900 is not a leap year but 2000 is.

The condition should actually be

if(year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
  // now we have a leap year.

furthermore, while I'm at it, you could do the following as well

Calendar cal = Calendar.getInstance();   
cal.set(Calendar.YEAR, year);   
if(cal.getActualMaximum(DAY_OF_YEAR) > 365)
  // leap year.

but that would be rather expensive to repeat 200 times, in comparison to the modulo-operations.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top