if statement - How do I return boolean in Java? -


public boolean isodd (int value) {     if ((value % 2)== 0){          return false;      } else if ((value % 2) > 0){          return true;      } }  

i error saying: private boolean isodd(int value) throws exception{ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method must return 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 returns true output.

i have no clue i'm doing wrong here!

your first code snippet causing error because have not catered else case. not need else if here want second condition execute in cases if statement not satisfied. try changing 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); } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -