Need Java Loop LeapYear help, please!

G

Guest

Guest
I was asked to write code that would check for leap year. I needed to create a loop, and enable user input. I also needed to put in an error for data entered less than 1582. It appears to be working, but when I enter a "3" or a "5" - instead of giving me the error message, it runs the check on them. I'm so new at this! Can someone please help me fix the bug - and figure out what I missed? Thank you!!!

import java.util.Scanner;

public class leapYear {

public static void main(String[] args) {

int leapYear;

Scanner scan = new Scanner(System.in);

System.out.println("Hey, welcome to my leap year checker!");
System.out.print("Please enter a year after 1582 or 0 to quit: ");
//leap year didn't exist before 1582//

leapYear = scan.nextInt();

while (leapYear != 0) {

//it's a leap year if the year is divisible by 4 but NOT by 100 //
//then if it's divisible by 400 //
//otherwise it's not a leap year //

if (leapYear < 1582) {
System.out.print("Leap year didn't exist prior to 1582. Please try again: ");
leapYear = scan.nextInt();
}

if((leapYear % 4 == 0 && leapYear % 100 != 0) || (leapYear % 400 == 0)) {
System.out.println("The year " + leapYear + " IS a leap year!");
} else {
System.out.println("The year " + leapYear + " is NOT a leap year.");
}

System.out.print("Try again? Please enter a year after 1582 or 0 to quit: ");
leapYear = scan.nextInt();
}
}
}
 
G

Guest

Guest
I just realized that I never thanked you!
I am so sorry for the delay - and thank you very, very much!
:D

 

ASK THE COMMUNITY

TRENDING THREADS