Java Code for calculating Leap Year

前端 未结 20 2538
执念已碎
执念已碎 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:10

    With the course of : TestMyCode Programming assignment evaluator, that one of the exercises was this kind of issue, I wrote this answer:

    import java.util.Scanner;
    
    public class LeapYear {
    
        public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            System.out.println("Type a year: ");
    
            int year = Integer.parseInt(reader.nextLine());
    
            if (year % 400 == 0 && year % 100 == 0 && year % 4 == 0) {
                System.out.println("The year is a leap year");
            } else
             if (year % 4 == 0 && year%100!=0 ) {
                System.out.println("The year is a leap year");
            } else 
            {
                System.out.println("The year is not a leap year");
            }
    
        }
    }
    

提交回复
热议问题