简体   繁体   English

为什么 eclipse 说我的方法没有返回有效的结果?

[英]Why eclipse is saying that my method doesn't return a valid result?

I've been doing this code in java for a Sudoku game since a while and I don't know what's wrong, Maybe is the "if" or de "For", but the IDE says that my method doesn't return a booelean type.一段时间以来,我一直在用 Java 为数独游戏编写此代码,但我不知道有什么问题,也许是“if”或 de“For”,但 IDE 说我的方法不返回布尔值类型。

// check if the number has already been used in the columns
private boolean checkColumns(int x, int y, int value) {
    for (int j = 0; j < 9; j++) {
        if (this.gridPlayer[j][y].getValue() == value) return false;
        else return true;
    }

    }
// Check if the number has already been used in the lines
private boolean checkLines(int x, int y, int value) {
    for (int i = 0; i <= 9; i++) {
        if (this.gridPlayer[x][i].getValue() == value) return false;
         else return true;
    }
    }

// Check if the number has already been used and the subGrid
private boolean checkSubGrid(int x, int y) {
    for (int i = 0; i <= 9; i++) {
        for (int j = 0; j <= 9; j++) {
            if (this.gridPlayer[x][y].getValueOfSubGrid(x, y) == this.gridPlayer[i][j].getValueOfSubGrid(i, j)) {
                if (this.gridPlayer[x][y].getValue() == this.gridPlayer[i][j].getValue()) {
                    return false;
                } else {
                    return true;
                }
            } else if (this.gridPlayer[x][y].getValueOfSubGrid(x, y) != this.gridPlayer[i][j].getValueOfSubGrid(i,
                    j)) {
                return true;
            }
        }
    }
}

The compiler is assuming that it is not 100% sure that the return statement from within your "for" loops will be called so it is seeing a path where your methods do not return any values even though they declare they do.编译器假设它不是 100% 确定将调用“for”循环中的 return 语句,因此它看到的路径是您的方法不返回任何值,即使它们声明它们返回。

You need to have some return value outside of your loops, even if you are sure this will never happen ie您需要在循环之外有一些返回值,即使您确定这永远不会发生,即

private boolean checkLines(int x, int y, int value) {
  for (int i = 0; i <= 9; i++) {
    if (this.gridPlayer[x][i].getValue() == value) return false;
     else return true;
  }
 return false; //even if you think it will never be run it is necessary 
}

Welcome,欢迎,
in your checkSubGrid() method you need to return a value if the runtime dont enter in the last else if : else if (this.gridPlayer[x][y]...) {在您的checkSubGrid()方法中,如果运行时没有在最后一个else if输入,您需要返回一个值: else if (this.gridPlayer[x][y]...) {

If the method is not void , you need to put a return.如果该方法不是void ,则需要返回。

 if(a > 1) {
   return a;
 } else {
   return b;
 }

In this case above we have a if - else statement, the method will always return true or false(or have a exception).在上面的这种情况下,我们有一个if - else语句,该方法将始终返回 true 或 false(或有异常)。

 if(a > 1) {
   return a;
 } else if(a == 0) {
   return b;
 }

In other hand, the method can or can not enter in the second if , them you have no returns.另一方面,该方法可以或不能进入第二个if ,他们没有回报。 You are not making sure that the compiler will have a return.您不能确保编译器会返回。

You can solve this putting a default return, or putting a else statement.您可以通过放置默认返回或放置 else 语句来解决此问题。

 if(a > 1) {
   return a;
 } else if(a == 0) {
   return b;
 } else {
   return null;
 }

Or或者

 if(a > 1) {
   return a;
 } else if(a == 0) {
   return b;
 }
 return null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM