Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code works for when the year is a leap year, but when it isnt i need the code to
find the next leap year and print it. The while loop i have now will keep counting the years eg:2002,2003,2004 an prints them. What can i use to have the while loop count the next year and put it through the evaluation process again until it hits the next leapyear. Can i put some sort of until statement in there? Thanks heaps guys


Java
 public static void main(String[] args) {

 int leapyear = 2001;
 boolean isleapyear;

 if (((leapyear % 4 == 0 && leapyear % 100 != 0||leapyear % 400 ==0)))
{
    isleapyear = true;
 }
 else
 {
     isleapyear = false;
 }
 if(isleapyear = true)
 {
    System.out.println(leapyear+" is a leapyear");
 }
 else
 {
     while(isleapyear=false)
     {
        leapyear = leapyear++;
         System.out.println(leapyear+ "is the next leapyear");
     }
 }
}
Posted
Updated 9-Sep-20 10:08am
v2

You need to create a function beside of the main-function and call it from the while.
The function should do the leap year check.

Use the already recommended break to break up the while loop if you only want to find the next one - don't do so if you want to find all (I would still limit that to a certain maxValue, cause the code would run forever otherwise).
 
Share this answer
 
Use break; keyword when it found next leap year in your while loop.

thanks,
Ambesha
 
Share this answer
 
Java
 if(isleapyear = true)

while(isleapyear=false)

A bit short of = signs in these two statements. Also your while loop should not contain the System.out.println() statement, otherwise it may claim that 2003 is a leap year.

[edit]
Actually that while loop is pointless, even after correcting the = to == it will not work.
[/edit]
 
Share this answer
 
v2
Just do a littel change in the end of code as shown.

i f ( i s l e a p y e a r = t r u e )
{ S y s t em . o u t . p r i n t l n ( l e a p y e a r + " i s a l e a p y e a r " ) ; } e l s e { leapyear = leapyear % 4 ; S y s t em . o u t . p r i n t l n ( l e a p y e a r + " i s t h e n e x t l e a p y e a r " ) ; } } }

explanation of code logic.
For example take 2014. Now 2014 mod 4=2, that is remainder when we divide 2014 with 4 is 2. Which means 2014 is 2 less than the next multiple of 4.
Similarlly leapyear is divisible with 4.
Note:- I don't use 'leapyear % 400' as it gives the first leapyear after 100 years.
Hope you get the answer and also understand its logic.
Warm regards.
Vikas Verma
 
Share this answer
 
I apologized for this. How can I've done such a mistake. Well anyway here is the correct one.
It is not
leapyear = leapyear % 4 ;
but
leapyear = leapyear+leapyear % 4 ;
Once again I'm really sorry for that silly mistake.
Hope now everything will be ok and you get the point, that I want to convey in last solution and also get logic behind that.
Thanks.
Vikas Verma
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900