Reputation: 1
My code seems to be working partially: What I mean is that when I don't enter a leap year for example 99 end then i choose 2 for february it prints 29 days,I would like to print 28 I have 2 separete methods, one to check if the year is leap or not and the other one to print the days in month so if someone know how to repair the program to be more efficient and for not leap year to display 28 days I would be grateful. Thank you.
Here is my code:
import java.util.Scanner;
public class LeapYearCheck
{
public static void main(String[] args)
{
LeapYearCheck.isLeapYear();
LeapYearCheck.daysInMonth();
}
static void isLeapYear()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if(year % 4 == 0 || year % 400 ==0)
{
System.out.println(year + " is leap year:");
}
else
{
System.out.println(year + " is not leap year:");
}
}
static void daysInMonth()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a month :");
int month = input.nextInt();
if (month == 2)
{
System.out.println("There are 29 days in February: ");
}
else if(month == 1)
{
System.out.println("The are 31 days in January ");
}
else if(month == 2)
{
System.out.println("The are 28 days in February ");
}
else if(month == 3)
{
System.out.println("The are 31 days in March ");
}
else if(month == 4)
{
System.out.println("The are 30 days in April");
}
else if(month == 5)
{
System.out.println("The are 31 days in May ");
}
else if(month == 6)
{
System.out.println("The are 30 days in June ");
}
else if(month == 7)
{
System.out.println("The are 31 days in July ");
}
else if(month == 8)
{
System.out.println("The are 31 days in August ");
}
else if(month == 9)
{
System.out.println("The are 30 days in September ");
}
else if(month == 10)
{
System.out.println("The are 31 days in October ");
}
else if(month == 11)
{
System.out.println("The are 30 days in November ");
}
else if(month == 12)
{
System.out.println("The are 31 days in December ");
}
else
{
System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
}
Upvotes: 0
Views: 198
Reputation: 27880
In addition to hovanessyan's suggestion, take into account that the information on whether the year is a leap year or not is kept and lost in the scope of the isLeapYear
function. This function should, for instance, return a boolean, and be called inside daysInMonth
, so daysInMonth
could return 28 or 29 accordingly. Using a Map
could also help simplify the huge if-else block.
Upvotes: 0
Reputation: 89169
It will always print
There are 29 days in February:
Because the first check is as follows:
if (month == 2)
{
System.out.println("There are 29 days in February: ");
}
Plus, the correct way to check leap year:
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
//Leap year.
}
Correct code:
if (month == 2) {
if (LeapYearCheck.isLeapYear()) {
System.out.println("There are 29 days in this month.");
} else {
System.out.println("There are 28 days in this month.");
}
}
Make your isLeapYear()
function return a boolean
(and not a void
).
Upvotes: 2
Reputation: 32831
You need to know the year as well as the month to compute the number of days:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if (LeapYearCheck.isLeapYear(year)) {
System.out.println(year + " is leap year:");
} else {
System.out.println(year + " is not leap year:");
}
System.out.println("Enter a month :");
int month = input.nextInt();
System.out.println("There are " + daysInMonth(year, month) + " in month");
}
static boolean isLeapYear(int year) {
return (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0);
}
static int daysInMonth(int year, int month) {
if (month == 1) {
return 31;
} else if (month == 2) {
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
} else if (month == 3) {
return 31;
} else if (month == 4) {
return 30;
} else if (month == 5) {
return 31;
} else if (month == 6) {
return 30;
} else if (month == 7) {
return 31;
} else if (month == 8) {
return 31;
} else if (month == 9) {
return 30;
} else if (month == 10) {
return 31;
} else if (month == 11) {
return 30;
} else if (month == 12) {
return 31;
} else {
throw new IllegalArgumentException(
"Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
Upvotes: 0
Reputation: 8868
import java.util.Scanner;
public class LeapYearCheck
{
public static boolean leap=false;
public static void main(String[] args)
{
LeapYearCheck.leap=LeapYearCheck.isLeapYear();
LeapYearCheck.daysInMonth();
}
static boolean isLeapYear()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if(year % 4 == 0 || year % 400 ==0)
{
System.out.println(year + " is leap year:");
return true;
}
else
{
System.out.println(year + " is not leap year:");return false;
}return false;
}
static void daysInMonth()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a month :");
int month = input.nextInt();
if(month == 1)
{
System.out.println("The are 31 days in January ");
}
else if(month == 2)
{if(leap==true){
System.out.println("The are 29 days in February ");}
else{System.out.println("The are 28 days in February ");}}
}
else if(month == 3)
{
System.out.println("The are 31 days in March ");
}
else if(month == 4)
{
System.out.println("The are 30 days in April");
}
else if(month == 5)
{
System.out.println("The are 31 days in May ");
}
else if(month == 6)
{
System.out.println("The are 30 days in June ");
}
else if(month == 7)
{
System.out.println("The are 31 days in July ");
}
else if(month == 8)
{
System.out.println("The are 31 days in August ");
}
else if(month == 9)
{
System.out.println("The are 30 days in September ");
}
else if(month == 10)
{
System.out.println("The are 31 days in October ");
}
else if(month == 11)
{
System.out.println("The are 30 days in November ");
}
else if(month == 12)
{
System.out.println("The are 31 days in December ");
}
else
{
System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
}
You can just check. I haven't compiled it
Upvotes: 0
Reputation: 15886
try this code....
import java.util.Scanner;
public class LeapYearCheck {
static boolean isleep;
public static void main(String[] args) {
LeapYearCheck.isLeapYear(); LeapYearCheck.daysInMonth();
}
static void isLeapYear() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if(year % 4 == 0 || year % 400 ==0)
{
isleep=true;
System.out.println(year + " is leap year:");
}
else
{
isleep=false;
System.out.println(year + " is not leap year:");
}
}
static void daysInMonth()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a month :");
int month = input.nextInt();
if (month == 2)
{
if(isleep)
System.out.println("There are 29 days in February: ");
else
System.out.println("There are 28 days in February: ");
}
else if(month == 1)
{
System.out.println("The are 31 days in January ");
}
else if(month == 3)
{
System.out.println("The are 31 days in March ");
}
else if(month == 4)
{
System.out.println("The are 30 days in April");
}
else if(month == 5)
{
System.out.println("The are 31 days in May ");
}
else if(month == 6)
{
System.out.println("The are 30 days in June ");
}
else if(month == 7)
{
System.out.println("The are 31 days in July ");
}
else if(month == 8)
{
System.out.println("The are 31 days in August ");
}
else if(month == 9)
{
System.out.println("The are 30 days in September ");
}
else if(month == 10)
{
System.out.println("The are 31 days in October ");
}
else if(month == 11)
{
System.out.println("The are 30 days in November ");
}
else if(month == 12)
{
System.out.println("The are 31 days in December ");
}
else
{
System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
}
Upvotes: 0
Reputation: 573
First of all u entered 29 days if month number is 2.
if (month == 2)
{
System.out.println("There are 29 days in February: ");
}
Upvotes: 0
Reputation: 81674
Simply put: the daysInMonth()
method needs to know what year it is, and then if the month number is 2, it needs to check if it's a leap year. Modify both your methods to accept the year and/or month as arguments rather than prompt for them, then prompt for the year and month in main()
and pass the necessary data to each method.
Upvotes: 0
Reputation: 31433
Your leap year check is wrong. You should use
if((year%100 != 0 && year%4 == 0) || year % 400 ==0))
Upvotes: 2