this method must return a result of type boolean, java

Multi tool use
Multi tool use


this method must return a result of type boolean, java


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?




5 Answers
5



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.


boolean


if



You could fix it like this (but only do 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;
}



The Java compiler doesn't make assumptions that a for loop will have an iteration or that an if statement block will run.


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



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.



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.



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.


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;
}
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

YpFSfbmFDrmpcSEoBfQhZqhwI5h,WctUqtzQSrBPryjZt,iL,7ldmnNlRXt
hzv,Fg kuP6vHdA4HBeoG4,M2NFk TqB7Pv86t9 6wWCT

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters