Java Code for calculating Leap Year

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

    This is what I came up with. There is an added function to check to see if the int is past the date on which the exceptions were imposed(year$100, year %400). Before 1582 those exceptions weren't around.

    import java.util.Scanner;
    
    public class lecture{
    
    
    public static void main(String[] args) {
        boolean loop=true;
        Scanner console = new Scanner( System.in );
        while (loop){
            System.out.print( "Enter the year: " );
    
            int year= console.nextInt();
            System.out.println( "The year is a leap year: "+ leapYear(year) );
            System.out.print( "again?: " );
            int again = console.nextInt();
            if (again == 1){
                loop=false;
            }//if
        }
    }
    public static boolean leapYear ( int year){
        boolean leaped = false;
        if (year%4==0){
            leaped = true;
            if(year>1582){
                if (year%100==0&&year%400!=0){
                    leaped=false;
                }
            }
        }//1st if
        return leaped;
    }
    } 
    

提交回复
热议问题