I have tried a few different methods, like print(boolean isLeapYear) and a few others, but I can not figure out how to get it to work. It always says that I have a missing class (boolean is primitive, does it need one?) Anyways, If the isLeapYear if-else statements are wrong, I’m not worried about those.. I just need to figure out how to print out the value of the boolean; any help / point in the right direction is greatly appreciated =]
import java.util.Scanner;
public class booleanfun {
boolean isLeapYear;
public static void main(String[] args)
{
System.out.println("Enter a year to determine if it is a leap year or not: ");
Scanner kboard = new Scanner(System.in);
int year = kboard.nextInt();
}
public boolean isLeapYear(int year)
{
if (year % 4 != 0)
isLeapYear = false;
else if ((year % 4 == 0) && (year % 100 == 0))
isLeapYear = false;
else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
isLeapYear = true;
else
isLeapYear = false;
System.out.println(isLeapYear);
System.out.println(boolean isLeapYear);
return isLeapYear;
}
}
should work just fine.
Incidentally, in
the
year % 400part will never be reached because if(year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)is true, then(year % 4 == 0) && (year % 100 == 0)must have succeeded.Maybe swap those two conditions or refactor them: