Java 错误:缺少 return 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13991196/
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
Java error: missing return statement?
提问by Selvart
Here is my method:
这是我的方法:
//usedLetters method to check if user's guess has already been guessed
private boolean usedLetters(char used[], char alphabet) throws IOException
{
for(int x=0; x<used.length; x++){
if(alphabet == used[x])
{
return true;
}
else
{
used[dataSize] = alphabet;
return false;
}
}
}//End of usedLetters method
IT checks to see if the alphabet that the user entered in an another method has already been guessed. If it has already been guessed, it returns true, and if has not been already guessed, it adds the alphabet into used, and returns false. But the error says that there are no return statements...I am not familiar with methods in general, so I am confused. Any help would be appreciated!
IT 检查用户在另一种方法中输入的字母表是否已经被猜到。如果已经猜到,则返回true,如果尚未猜到,则将字母添加到used中,并返回false。但是报错说没有返回语句……我对方法一般不熟悉,所以我很困惑。任何帮助,将不胜感激!
回答by MrSmith42
What if used.length==0
? Then the for-loop is never entered.
万一used.length==0
呢?然后永远不会进入 for 循环。
Therefore you need a return
statement after the loop.
因此,您需要return
在循环后声明。
回答by amit
What if the for is never entered? i.e. used.length == 0
(used
is an empty array). In this case - nothing will be returned.
如果从未输入 for 怎么办?即used.length == 0
(used
是一个空数组)。在这种情况下 - 不会返回任何内容。
The compiler forbids a flow that can terminate without returning the value, and that's why it shows the error.
编译器禁止可以在不返回值的情况下终止的流,这就是它显示错误的原因。
Also note, I believe even after fixing this issue - the program will yield a wrong result, it will only check the first element of used
, without advancing to the next one.
另请注意,我相信即使在解决此问题后 - 程序也会产生错误的结果,它只会检查 的第一个元素used
,而不会前进到下一个元素。
回答by Jimmy Thompson
You should move the return false;
to just before the last }
. Otherwise it will return in the first iteration of the loop.
您应该将return false;
to移动到最后一个 之前}
。否则它将在循环的第一次迭代中返回。
回答by Pradeep Simha
/usedLetters method to check if user's guess has already been guessed
private boolean usedLetters(char used[], char alphabet) throws IOException
{
for(int x=0; x
if(alphabet == used[x])
{
return true;
}
else
{
used[dataSize] = alphabet;
return false;
}
}
}//End of usedLetters method
}//usedLetters方法结束
There should be return statement after for loop, currently there is no return statement that's why you are getting error.
for 循环之后应该有 return 语句,目前没有 return 语句,这就是为什么你会收到错误。
回答by Ivaylo Strandjev
As you have written your code, the method will always return on the first iteration of the cycle, while I doubt this is what you want.
当您编写代码时,该方法将始终在循环的第一次迭代时返回,而我怀疑这是否是您想要的。
I believe that you should return false outside the for. I am not sure what does to "guess an alphabet" mean but I think this is what you are trying to do.
我相信你应该在 for 之外返回 false。我不确定“猜字母表”是什么意思,但我认为这就是你想要做的。
回答by Jamie
Yep, all the above are correct. You would be better off defining the return variable at the top, setting it to values wherever you need to within the method body, breaking out of the 'for' loop and returning it once at the end.
是的,以上都是正确的。您最好在顶部定义返回变量,将其设置为方法主体内需要的任何值,跳出“for”循环并在最后返回一次。