public boolean isOdd (int value) {
if ((value % 2)== 0){
return false;
} else if ((value % 2) > 0){
return true;
}
}
I get an error saying: private boolean isOdd(int value) throws Exception{ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method must return a result of type boolean
I tried doing:
public boolean isOdd (int value) {
boolean isOdd = ((value % 2) > 0);
return true;
}
public boolean isEven (int value) {
boolean isEven = ((value % 2) > 0);
return true;
}
and it only returns true as an output.
I have no clue what I'm doing wrong here!
Your first code snippet is causing an error because you have not catered for the else
case. You do not need an else if
here as you want the second condition to execute in all cases where the if statement is not satisfied. Try changing it to:
public boolean isOdd (int value) {
if ((value % 2)== 0){
return false;
}
else { return true; }
}
or more simply:
public boolean isOdd (int value) {
return ((value % 2) != 0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With