Problems with Java view year code

import java.util.Scanner; public class Hw2JamesVaughn { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter a year: "); int year = input.nextInt(); if((year < 1582) == (year % 4==0)) System.out.println(year + " is a leap year"); else System.out.println(year + " is not a leap year"); if((year > 1582) == (year % 100 != 0) || (year % 400 == 0)) System.out.println(year + " is a leap year"); else System.out.println(year + " is not a leap year"); } } 

This is the destination.

(To determine if a particular year is a leap year, use the following logic:

  • year should be divisible by 4
  • starting from 1582, if the year is divided by 100, it should also be divided by 400. Thus, 1700 is not a leap year, but 2000. However, 1500 is a leap year since it was until 1582, the year the Gregorian calendar was adopted. Your program will ask for the year, and then display whether the year is a leap year or not.)

I went this far with my java leap year program, but it doesn’t work! I worked on this, and I have no idea what is wrong.

+6
source share
2 answers

First, this if((year < 1582) == (year % 4==0)) checks for logical equality. I think you wanted if((year < 1582) && (year % 4==0)) , but I'm afraid it still doesn't fix your logic.

I suggest you start by creating a method. The first part should check if year less than 1582. If so, return true if it is a multiple of 4. The second part is well described in Wikipedia here . The combination gives something like:

 private static boolean isLeapYear(int year) { if (year < 1582) { return (year % 4 == 0); } /* * Rest of algorithm from: http://en.wikipedia.org/wiki/Leap_year */ if (year % 4 != 0) { /* * if (year is not divisible by 4) then (it is a common year) */ return false; } else if (year % 100 != 0) { /* * else if (year is not divisible by 100) then (it is a leap year) */ return true; } /* * else if (year is not divisible by 400) then (it is a common year) * else (it is a leap year) */ return (year % 400 == 0); } 

Then you can use printf to output the result,

 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a year: "); int year = input.nextInt(); System.out.printf("%d %s leap year", year, isLeapYear(year) ? "is a" : "is not a"); } 

Finally, your source code can be implemented as -

 if (year < 1582 && year % 4 == 0) System.out.println(year + " is a leap year"); else if (year < 1582) System.out.println(year + " is not a leap year"); else if (year >= 1582 && (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"); 
+1
source

Besides the algorithm, you can calculate the leap year using the java built-in Calendar api.

 static boolean isLeapYear(int year){ Calendar calendar= Calendar.getInstance(); calendar.set(Calendar.YEAR,year); return calendar.getActualMaximum(Calendar.DAY_OF_YEAR) > 365; } 
+1
source

Source: https://habr.com/ru/post/974659/


All Articles