Results 1 to 2 of 2
  1. #1
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default return method not working?

    Heres my code:
    Java Code:
    	private static int high() {
    		if(n1 >= n2 && n1 >= n3 && n1 >= n4){
    			return 0;
    		}else{
    			if(n2 >= n1 && n2 >= n3 && n2 >= n4){
    				return 1;
    			}else{
    				if(n3 >= n1 && n3 >= n2 && n3 >= n4){
    					return 2;
    				}else{
    					if(n4 >= n1 && n4 >= n2 && n4 >= n3){
    						return 3;
    				}
    			}
    		}
    	}	
    }
    The error says that i need to add a return statement, although i already have them?
    Thanks!

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: return method not working?

    If you follow your if statements you will see that it is possible that a return statement will not be reached.
    Java Code:
    private static int high() {
            if(n1 >= n2 && n1 >= n3 && n1 >= n4){//false
                return 0;
            }else{
                if(n2 >= n1 && n2 >= n3 && n2 >= n4){
                    return 1;
                }else{
                    if(n3 >= n1 && n3 >= n2 && n3 >= n4){
                        return 2;
                    }else{
                        if(n4 >= n1 && n4 >= n2 && n4 >= n3){// if you get here and it evaluates to false the method ends with no return statement
                            return 3;
                    }
                }
            }
        }   
    }

Similar Threads

  1. What can a method return?
    By fatabass in forum New To Java
    Replies: 8
    Last Post: 01-29-2012, 03:22 PM
  2. Why is this method return 0
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 06-30-2011, 07:21 PM
  3. cant return value from a method
    By gedas in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 08:37 AM
  4. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 11:49 AM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •