Hi this is my first post, but I've lurked for a while usually finding answers to my questions in other threads, I couldn't find this one so I broke my posting cherry to ask.
I know I'm missing something, probably really simple, but possibly I'm doing this completely wrong.
I'm trying to write a method that determines if a year is a leap year and then returns it to main to print out whether it's a leap year or not, after I get this working I'm going to modify it so that it runs in a loop until the user exits, but for now I'm having an issue getting it to just work correctly.
I've tried it a few different ways, they all compile, but they all return the same thing, which is the output for the "true" value of the boolean "That is a leap year!" no matter what year I put in.
first try
import java.text.*; //formatted output
import java.util.Scanner; //keyboard input
public class leapyear
{
public static boolean isLeapYear()
{
double year;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a year >");
year = keyboard.nextDouble();
if ((year % 4 == 0) && (year % 100 != 0))
return true;
if
(year % 400 == 0)
return true;
else
return false;
}
public static void main(String[] args)
{
boolean isLeapYear = isLeapYear();
if (isLeapYear = true)
System.out.print("That is a leap year!");
if (isLeapYear = false)
System.out.print("That is not a leap year.");
}
}
second try
import java.text.*; //formatted output
import java.util.Scanner; //keyboard input
public class leapyear
{
public static boolean isLeapYear()
{
double year;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a year >");
year = keyboard.nextDouble();
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}
public static void main(String[] args)
{
boolean isLeapYear = isLeapYear();
if (isLeapYear = true)
System.out.print("That is a leap year!");
else
System.out.print("That is not a leap year.");
}
}
I think it's a problem with the boolean method, but I'm really inexperienced and I can't quite figure it out.
Thanks for taking a look :)