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

ios - iPhone/iPad different view orientations in different views , and apple approval process -

monitor web browser programmatically in Android? -

c# - Using multiple datasets in RDLC -