TierOne TierOne - 27 days ago 20
Java Question

Unreachable statement with Java

I'm getting an Unreachable statement with this code, however I don't see where I am wrong ...

public static int daysInMonth(int month, boolean inLeapYear) {
if (month == 1) {
return 31;
}
if (month == 2 && inLeapYear) {
return 29;
}
else {
return 28;
}
if (month % 2 == 0) {
return 30;
}
else {
return 31;
}
}

Answer

Your second if statement is followed by an else that contains a return statement.

The line if(month % 2 == 0) and onwards will never be executed

Comments