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 assignment.
(To determine if a particular year is a leap year, use the following logic:
I have gotten this far with my java leap year program but its not working! Ive been working on this and i have no idea what is wrong.
Check if the number is evenly divisible by 400 to confirm a leap year. If a year is divisible by 100, but not 400, then it is not a leap year. If a year is divisible by both 100 and 400, then it is a leap year. For example, 1900 is evenly divisible by 100, but not 400 since it gives you a result of 4.75.
The rule is that if the year is divisible by 100 and not divisible by 400, leap year is skipped. The year 2000 was a leap year, for example, but the years 1700, 1800, and 1900 were not. The next time a leap year will be skipped is the year 2100.
Firstly, this if((year < 1582) == (year % 4==0))
checks boolean equality. I think you wanted an if((year < 1582) && (year % 4==0))
but I'm afraid that still doesn't fix your logic.
I suggest you start by creating a method. The first part should test if the year
is less then 1582. If so, return true if it's a multiple of 4. The second part is well described on Wikipedia here. Putting it together 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 original code could be implemented like -
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With