Java Code for calculating Leap Year

前端 未结 20 2275
执念已碎
执念已碎 2020-11-22 16:30

I am following \"The Art and Science of Java\" book and it shows how to calculate a leap year. The book uses ACM Java Task Force\'s library.

Here is the code the boo

20条回答
  •  孤街浪徒
    2020-11-22 17:17

        import java.util.Scanner;
    
        public class LeapYear {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the year then press Enter : ");
            int year = input.nextInt();
    
            if ((year < 1580) && (year % 4 == 0)) {
                System.out.println("Leap year: " + year);
            } else {
                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                    System.out.println("Leap year: " + year);
                } else {
                    System.out.println(year + " not a leap year!");
                }
    
            }
        }
    }
    

提交回复
热议问题