Jump to content

Help with calculating leap year(java)

yusup31
   int x = -3 ;
      	Scanner scan = new Scanner(System.in); 
      			
 System.out.println("enter year that you want to know if it is a leap year( type -3 to quit): ");
  
 int y = scan.nextInt();
 while(y != x)
 	
 if(y >= 1582 )
 	{
 	 System.out.println("this year is not a leap year.");
 	}

Else if (y >= 1582 && (y%4==0 && y%100 == 0 && y%400 !=0 ))
 		{
 		 System.out.println("this year is a leap year.");	
	
		}
		else if (y>=1582 && (y%4==0 && y%100!=0))
	{System.out.println("this year is a leap year.");	
	}
		else (y <= 1582)
		 {
		 System.out.println("error Gregorian calendar not adopted until 1582");	
		 }
	
 	 

I am writing this program for my coding class and I cant seem to get my else if statements to be correct. When compiling the code it says that the last else has no if. any help is appreciated 

Link to comment
Share on other sites

Link to post
Share on other sites

else cannot have a condition following it (y <= 1582). Remove this line and the program will be syntactically valid. I did not check the programs logic, but that is why it will not run as-is.

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Erik Sieghart said:

Why would he put in a condition if he didn't want the code to execute on that condition?

Because that condition doesn't make sense (nor do the other 2 else ifs.)  If y isn't >= 1582 (failing the first if) than the last else if is always true which makes the condition meaningless.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×