Java 缺少 if/else 语句的 return 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20231393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing return statement for if/else statement
提问by user3027772
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
为什么这会给我一个丢失的返回语句错误?每个 if/else 都有一个返回,最后一个括号出现错误
回答by nhgrif
Every 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.
每个return
语句都在if
语句中。虽然在逻辑上可能是不可能的,但编译器需要在没有任何if
评估为真时返回。
I recommend:
我建议:
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.
抛出异常将允许您在不返回任何内容的情况下退出该方法......因为可以说,如果您进入 final else
,显然出了问题。
回答by Christian
You could try changing the las else if
to else
.
您可以尝试将 las 更改else if
为else
.
if (number == randomnumber)
return 0;
else if (number < randomnumber)
return -1;
else
return 1;
回答by aw04
You need an else for that to work as you expect, it does not evaluate your conditions to know that it will always return
您需要一个 else 才能按预期工作,它不会评估您的条件以知道它总是会返回
回答by Paul Samsotha
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
返回应else
在整个内或外进行if/else-if
。根据条件,可能永远不会对所有退货进行退货
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:
如果条件 1、2 或 3 都不满足,则不会返回。应始终提供退货。所以修复方法是:
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;
回答by David Schwartz
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.
编译器不需要能够确定您的if
/else
树是否涵盖了所有可能的情况。如果你能写出一些编译器足够聪明以判断出没问题而其他编译器没问题的代码,那就太糟糕了。Java 要求您编写清晰、干净的代码,而不是对编译器的智能程度进行精确的规范。
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.
根据 Java 规范中的定义,函数的右花括号是可达的。您可以证明它无法通过“可达”的其他定义到达并不重要。Java 规范中“可达”的概念是规范中详细解释的正式概念,编译器可以实际实现。这不是“可达”的常识概念,无论如何都无法教给计算机。
回答by Andrew Gies
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.
Java 要求保证非空方法返回一些东西(即使它是空的)。如果有 if 语句,根据定义,只有有时您的 return 语句会运行。
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.
else 语句不是必需的,但它确实使 imo 更容易理解。
回答by Dave Lillethun
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
to just an 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.)
基本上,编译器不够聪明,无法意识到这number == randomnumber || number < randomnumber || number > randomnumber
是一个重言式。由于您没有else
,它认为您可能会通过条件,然后在不返回的情况下到达函数末尾。最简单的解决方法是将最后elseif
一个更改为else
. 您从前面的条件中知道,number > randomnumber
如果达到该点,则必须为真。(如果您偏执并想确定,您可以添加断言。)
回答by Glenn Teitelbaum
There is no need for an else
after a return
statement:
不需要else
afterreturn
语句:
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.
请注意,这解决问题的原因是编译器不检查逻辑上必须返回的解决方案。例如,虽然 A 必须 > < 或 = 到 B,但它不会检查这种关系。它正在寻找明确覆盖每条路径的可能性。