First, this if((year < 1582) == (year % 4==0)) checks for logical equality. I think you wanted if((year < 1582) && (year % 4==0)) , but I'm afraid it still doesn't fix your logic.
I suggest you start by creating a method. The first part should check if year less than 1582. If so, return true if it is a multiple of 4. The second part is well described in Wikipedia here . The combination gives something like:
private static boolean isLeapYear(int year) { if (year < 1582) { return (year % 4 == 0); } /* * Rest of algorithm from: http://en.wikipedia.org/wiki/Leap_year */ if (year % 4 != 0) { /* * if (year is not divisible by 4) then (it is a common year) */ return false; } else if (year % 100 != 0) { /* * else if (year is not divisible by 100) then (it is a leap year) */ return true; } /* * else if (year is not divisible by 400) then (it is a common year) * else (it is a leap year) */ return (year % 400 == 0); }
Then you can use printf to output the result,
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a year: "); int year = input.nextInt(); System.out.printf("%d %s leap year", year, isLeapYear(year) ? "is a" : "is not a"); }
Finally, your source code can be implemented as -
if (year < 1582 && year % 4 == 0) System.out.println(year + " is a leap year"); else if (year < 1582) System.out.println(year + " is not a leap year"); else if (year >= 1582 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) System.out.println(year + " is a leap year"); else System.out.println(year + " is not a leap year");
source share