I have a serious question that's been bugging me for the longest time now. I've recently been assigned to do the Logic-1 > alarmClock on CodingBat and I came up with this code as my solution:
public String alarmClock(int day, boolean vacation) {
if (!vacation) {
if (0 < day && day != 6) {
return "7:00";
} else {
return "10:00";
}
}
if (vacation) {
if (0 < day && day != 6) {
return "10:00";
} else{
return "off";
}
}
}
When I go to run this code I keep getting this message saying "Compile Problems: missing return statement line:18." After tweaking the code and trying other solutions I gave up and eventually asked a friend how they solved the code to which they showed me this code:
public String alarmClock(int day, boolean vacation) {
if (!vacation) {
if(0 < day && day != 6)
return "7:00";
else
return "10:00";
} else if (day > 0 && day != 6)
return "10:00";
else
return "off";
}
He gave me a few suggestions but the one I 've been curious about is why the second example doesn't have the curly brackets. I was taught that an if statement was to have the curly brackets to contain the body of code within the block and at this point I'm confused why the second code doesn't have any curly brackets next to some of the if/else statements.