此方法必须返回布尔类型的结果,java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20336996/
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
this method must return a result of type boolean, java
提问by user2983594
public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]
) {
return true;
}
}
for(int i=0; i<7;i+=3){
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;}
}
}
It returns me this error: this method must return a result of type boolean. What am I doing wrong?
它返回给我这个错误:这个方法必须返回一个布尔类型的结果。我究竟做错了什么?
采纳答案by musical_coder
Right now, the function isn't guaranteed to return a boolean
, because it's possible that neither of the if
statements will ever be entered.
现在,该函数不能保证返回 a boolean
,因为可能if
永远不会输入这两个语句。
You could fix it like this (but onlydo this if it's actually what your logic needs!):
你可以像这样修复它(但只有当它实际上是你的逻辑需要时才这样做!):
public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]
) {
return true;
}
}
for(int i=0; i<7;i+=3){
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;}
}
return false;
}
回答by Nathan Hughes
All possible ways that the method can exit need to return something. If your code makes it through both for loops without having an if condition evaluate to true, then you need a return at the end that specifies what gets returned.
方法退出的所有可能方式都需要返回一些东西。如果您的代码在没有 if 条件评估为 true 的情况下通过两个 for 循环,那么您需要在最后一个 return 指定返回的内容。
回答by rgettman
The Java compiler doesn't make assumptions that a for
loop will have an iteration or that an if
statement block will run.
Java 编译器不假设for
循环将进行迭代或if
语句块将运行。
There are execution paths where there is no return
statement. What happens if the execution path doesn't execute any of the existing return
statements and drops to the bottom? There isn't a return
there.
有没有return
语句的执行路径。如果执行路径不执行任何现有return
语句并下降到底部会发生什么?那里没有return
。
Add a return
at the bottom.
return
在底部添加一个。
回答by Nick Div
The compiler is not aware that at least one of the loops will be executed. It takes into account all the possibilities of the execution and one of them is that neither of the loops will be executed and in that case there is no return statement. So insert a return statement out of the loops as well.
编译器不知道将执行至少一个循环。它考虑了执行的所有可能性,其中之一是两个循环都不会被执行,在这种情况下没有 return 语句。因此,还要从循环中插入一个 return 语句。
回答by Prometal328
The answer to this question is easy. It happened to me too. The problem in your code is that you don't say to the computer what to do in case that the "if" statement is wrong, so you just have to add an "else {return false}" to every "if". Another tip is: please make your code cleaner and readable.
这个问题的答案很简单。它也发生在我身上。您代码中的问题是您没有告诉计算机在“if”语句错误的情况下该怎么做,因此您只需为每个“if”添加一个“else {return false}”。另一个提示是:请使您的代码更清晰易读。
public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]) {
return true;
} else {
return false;
}
}
for (int i=0; i<7; i+=3) {
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;
} else {
return false;
}
}
}