Advertisement
quytm2239

Untitled

Sep 30th, 2020
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         int year;
  8.         boolean isLeapYear = false;
  9.         Scanner scan = new Scanner(System.in); // Initialize a Scanner with system InputStream
  10.         System.out.println("----Program checking the leap year----");
  11.  
  12.         while (true) {
  13.             System.out.println("Enter year: ");
  14.             try {
  15.                 year = scan.nextInt(); // Wait to read user input to Integer/int
  16.             } catch (Exception e) {
  17.                 scan.nextLine(); // use this to clear previous scan.next.. buffer
  18.                 continue;
  19.             }
  20.  
  21.             if (year % 4 == 0) { // divisible 4 - leap year
  22.                 if (year % 100 == 0) { // divisible both 4 and 100 is not leap year
  23.                     if (year % 400 == 0) // divisible 400 is leap year
  24.                         isLeapYear = true;
  25.                     else
  26.                         isLeapYear = false; // not divisible 400 is not leap year
  27.                 } else { // divisible 4 but not divisible 100 - leap year
  28.                     isLeapYear = true;
  29.                 }
  30.             } else { // not leap year
  31.                 isLeapYear = false;
  32.             }
  33.  
  34.             if (isLeapYear)
  35.                 System.out.println(year + " is a leap year.");
  36.             else
  37.                 System.out.println(year + " isn't a leap year.");
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement