Declaración de retorno faltante para la declaración if/else

public int checkGuess(int g, int randomnumber){

    number=g;
    this.randomnumber= randomnumber;

    if (number == randomnumber)
        return 0;

    else if (number < randomnumber)
        return -1;

    else if (number > randomnumber)
        return 1;

}

why is this giving me a missing return statment error? every if/else has a return the error comes up for the last bracket

preguntado el 27 de noviembre de 13 a las 01:11

a return should be made inside an else or outside the entire if/else-ifs -

Because you're missing a return statement for the implied else. -

8 Respuestas

Cada return statement is inside an if statement. While it may be logically impossible as written, the compiler needs a return for when none of the if evaluate true.

Yo recomiendo:

public int checkGuess(int number, int randomnumber){
    int retVal = 0;
    this.randomnumber= randomnumber;

    if (number == randomnumber) {
        retVal = 0;
    } else if (number < randomnumber) {
        retVal = -1;
    } else if (number > randomnumber) {
        retVal = 1;
    }
    return retVal;
}

This solution fixes the compiler problem and improves readability slightly, in my opinion.


Alternatively, there's this solution:

public int checkGuess(int number, int randomnumber){
    this.randomnumber= randomnumber;

    if (number == randomnumber) {
        return 0;
    } else if (number < randomnumber) {
        return -1;
    } else if (number > randomnumber) {
        return 1;
    } else {
        //throw an exception
    }
}    

Throwing an exception will allow you to get out of the method without returning anything... because arguably, if you get to the final else, something clearly went wrong.

respondido 27 nov., 13:01

+1 for suggesting throwing an exception, which is really the best thing to do when you're that confident the code is unreachable. - Odisea de vuelo

+1 for me throwing IllegalArgumentException is the best if you think the method should not entertain value that will not satisfy one of the defined conditions. - Brdo

Just because the compiler can't figure out that there are ONLY three posssibilities doesn't mean that the programmer can't - Glenn Teitelbaum

And regardless of whether or not the programmer has figured this out, the compiler is the one that matters... he's the one that compiles your code. - nhgrif

You could try changing the las else if a else.

if (number == randomnumber)
    return 0;

else if (number < randomnumber)
    return -1;

else
    return 1;

respondido 27 nov., 13:01

yeah i know that works, but i think not saying that if number is greater return 1; messes up the tester file. any way to still have that condition? - Stock_Clue

The compiler is not required to be able to figure out whether or not your if/else tree covers every possible case. And it would be awful if you could write code that some compilers were smart enough to figure out were okay and other compilers weren't. Rather than having a precise specification for exactly how smart a compiler has to be, Java requires you to write clear, clean code.

The closing curly brace of the function is, by the definition in the Java specification, reachable. That you can prove that it is not reachable by some other definition of "reachable" doesn't matter. The Java specification notion of "reachable" is a formal notion explained in detail in the specification that compilers can actually implement. It is not the common sense notion of "reachable", which one could never teach to a computer anyway.

respondido 27 nov., 13:01

Basically, the compiler is not smart enough to realzie that number == randomnumber || number < randomnumber || number > randomnumber is a tautology. Since you don't have an else, it thinks it's possible you will get past the conditional and then hit the end of function without returning. The easiest fix is to change your last elseif a solo un else. You know from the previous conditions that number > randomnumber must be true if you get to that point. (You could add an assert if you're paranoid and want to be sure.)

respondido 27 nov., 13:01

No hay necesidad de else después de return declaración:

if (number == randomnumber)
  return 0;

if (number < randomnumber)
  return -1;

// Must be true: if (number > randomnumber)
return 1;

Note that the reason this solves the problem is because the compiler does not check for solutions that logically must return. e.g. while A must be > < or = to B, it does not check for that relationship. It is looking for possibilities that cover every path explicitly.

respondido 12 nov., 15:22

Aunque se cree que else following returns isn't necessary, it helps for readability (especially in cases where the if body is more that a simple return 0;. But either way, that part is irrelevant to the actual problem (though your posted code does solve the problem...just doesn't explain the problem, leaving a misleading answer). - nhgrif

@nhgrif Added Note to explain why, also {} make this clearer, with or without the else. I didn't add them to highlight the difference I was presenting. - Glenn Teitelbaum

@nhgrif I believe the code is more readable and logical when ELSE is not used after IF-return declaración. - Zoom máximo

You need an else for that to work as you expect, it does not evaluate your conditions to know that it will always return

respondido 27 nov., 13:01

a return should be made inside an else or outside the entire if/else-if. It's possible a return is never made with all returns depending on a condition

if (condition 1)
    return a;
else if (condition 2)
    return b;
else if (condition 3)
    return c;

If neither condition 1, 2, or 3 are met there will no return. A return should always be made available. So a fix would be:

if (condition 1)
    return a;
else if (condition 2)
    return b;
else if (condition 3)
    return c;
else
    return d;

Or

if (condition 1)
    return a;
else if (condition 2)
    return b;
else if (condition 3)
    return c

return d;

respondido 27 nov., 13:01

Java requires that non-void methods are guaranteed to return something (even if it's null). If there is an if statement, by definition, only sometimes your return statements are run.

if(number == randomnumber) {
    return 0;
} else if(number < randomnumber) {
    return -1;
} else if(number > randomnumber) {
    return 1;
} else {
    return -2;
}

The else statement isn't required, but it does make it easier to understand imo.

respondido 27 nov., 13:01

else return -2; is pretty sloppy here, in my opinion and hinders readability. This answer does solve the compiler problem though. - nhgrif

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.