此方法必须返回boolean类型,java的结果 [英] this method must return a result of type boolean, java

查看:2027
本文介绍了此方法必须返回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;}
    }
}

它返回我这个错误:此方法必须返回boolean类型的结果。我在做什么错了?

It returns me this error: this method must return a result of type boolean. What am I doing wrong?

推荐答案

目前,该功能不能保证返回布尔,因为它可能是既不在如果语句将永远不会进入。

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

这篇关于此方法必须返回boolean类型,java的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆