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

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

推荐答案

目前,该函数不保证返回 boolean,因为可能没有 if 语句将永远被输入.

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天全站免登陆