this method must return a result of type boolean, java

前端 未结 5 1272
孤独总比滥情好
孤独总比滥情好 2020-11-28 16:04
 public boolean Winner() {
    for (int z = 0; z < 3; z++) {
            if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]
         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 16:39

    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.

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

提交回复
热议问题