# if else statements

• October 24th, 2010, 12:42 PM
mozyman
if else statements
I need to be able to input two numbers, corresponding to a month and year, and have it return the number of days in that month. It all works except for when it gets to february and whether its a leap year or not. Currently it outputs 29, no matter what the year. I think it's something to do with the way the if and else statements are contained in each other, but not sure. Only recently started learning java so. Perhaps someone could give me a hand...

The isACenturyYear(year) and isDivisibleBy(year,400) are working methods already set above.

Code Java:

``` public int daysInMonth(int month, int year) { int daysInMonth ; daysInMonth = 0 ; boolean leap ; leap = false ; if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { daysInMonth = 31 ; } else { if (month == 4 || month == 6 || month == 9 || month == 11) { daysInMonth = 30 ; } else { if(isACenturyYear(year) || month == 2) { leap = isDivisibleBy(year,400) ; } else { leap = isDivisibleBy(year,4) ; } daysInMonth = 29 ; if (isACenturyYear(year) || month == 2) { leap =! isDivisibleBy(year,400) ; } else { leap =! isDivisibleBy(year,4) ; } daysInMonth = 28 ; } } return daysInMonth ; }```
• October 24th, 2010, 02:18 PM
DavidFongs
Re: if else statements
After you assign leap to either true or false, where in your code are you using that value to make decisions? Fix that, and the solution should be obvious
• October 24th, 2010, 03:35 PM
mozyman
Re: if else statements
Quote:

Originally Posted by DavidFongs
After you assign leap to either true or false, where in your code are you using that value to make decisions? Fix that, and the solution should be obvious

I'm not overly sure to be honest :/

Here's the methods that were already set, I'm just not sure how to use them...

Code :

``` public boolean isDivisibleBy(int x, int y) { boolean answer = false ; if(x % y == 0) { answer = true ; } return answer ; }   public boolean isACenturyYear(int year) { boolean answer = false ; if(year % 100 == 0) { answer = true ; } return answer ; }   public boolean isLeapYear(int year) { boolean answer ; if(isACenturyYear(year)) { answer = isDivisibleBy(year,400) ; } else { answer = isDivisibleBy(year,4) ; } return answer ; }```

I assumed it would check that if the value of leap is equal to that of isDivisibleBy(year,400) or isDivisibleBy(year,400), it would set the value for daysInMonth, otherwise would set different value.

Also after seeing, I could probably replace some of that code with just the isLeapYear method yes?
• October 24th, 2010, 03:54 PM
DavidFongs
Re: if else statements
You are making this overly complicated. If the month is february, set daysInMonth to 28, then call isLeapYear, and if that is true, add one
• October 24th, 2010, 08:06 PM
mozyman
Re: if else statements
Ahh finally got it to work :) wasn't overly sure of how to call isLeapYear, got it through a bit of trial and error though.