Jevon Çøö Jevon Çøö - 1 month ago 7
Java Question

Leaper if expression is faulty

import java.util.*;

public class LeapYear {

public static void main (String args[]) {

Scanner scan = new Scanner(System.in);
int userInput = scan.nextInt();
boolean leapYearisTrue = false;

while ( userInput != 0 ) {
if (userInput % 4 == 0) {
if ( (userInput % 100 == 0) && (userInput % 400 != 0) ) {
leapYearisTrue = false;
System.out.println (leapYearisTrue);
}
else {
leapYearisTrue = true;
System.out.println (leapYearisTrue);
}
userInput = scan.nextInt();
}
}
}
}


Whenever I input a value that IS a leap year, the program runs smoothly and does what it's supposed to:

2000
true
1960
true
400
true


but whenever I input a non-leap year, it doesn't print false and would no longer print that a number is a leap year:

403
400
2000 ( this is a leap year , yet it doesn't print true)
2004

Answer

You need to add an else condition to if (userInput % 4 == 0) condition.

Comments