Java Code for calculating Leap Year

前端 未结 20 2269
执念已碎
执念已碎 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:06
    boolean leapYear = ( ( year % 4 ) == 0 );
    
    0 讨论(0)
  • 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");
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-11-22 17:12

    You can ask the GregorianCalendar class for this:

    boolean isLeapyear = new GregorianCalendar().isLeapYear(year);
    
    0 讨论(0)
  • 2020-11-22 17:13

    easiest way ta make java leap year and more clear to understandenter code here

    import  java.util.Scanner;
    

    class que19{

    public static void main(String[] args) {
    
        Scanner input=new Scanner(System.in);
    
        double a;
    
        System.out.println("enter the year here ");
        a=input.nextDouble();
        if ((a % 4 ==0 ) && (a%100!=0) || (a%400==0)) {
            System.out.println("leep year");
    
        }
        else {
            System.out.println("not a leap year");
        }
    }
    

    }

    0 讨论(0)
  • 2020-11-22 17:14

    Pseudo code from Wikipedia translated into the most compact Java

    (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
    
    0 讨论(0)
  • 2020-11-22 17:15

    Your code, as it is, without an additional class, does not appear to work for universal java. Here is a simplified version that works anywhere, leaning more towards your code.

    import java.util.*;
    public class LeapYear {
        public static void main(String[] args) {
            int year;
            {
                Scanner scan = new Scanner(System.in);
                System.out.println("Enter year: ");
                year = scan.nextInt();
    
                if ((year % 4 == 0) && year % 100 != 0) {
                    System.out.println(year + " is a leap year.");
                } else if ((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.");
                }
            }
        }
    }
    

    Your code, in context, works just as well, but note that book code always works, and is tested thoroughly. Not to say yours isn't. :)

    0 讨论(0)
提交回复
热议问题