此方法必须返回boolean(Java)类型的结果 [英] This method must return a result of type boolean(Java)

查看:1347
本文介绍了此方法必须返回boolean(Java)类型的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。

boolean checkHit2() {
    if (cx < 0 || cx >= 640) {return true;}
    if (cy < ground[(int)cx]) {return false;}
    if (cx < blue + 15 && cx > blue - 15) {
        score = (int)score + 1;

我做错了什么?它给我错误消息此方法必须返回类型为boolean的结果。请帮忙。

What am I doing wrong? it gives me the error message "This method must return a result of type boolean". Please help.

推荐答案


此方法必须返回boolean类型的结果

"This method must return a result of type boolean"

表示你的方法应该为每个案例返回 boolean 值。目前,如果你的一个if条件是 true ,它将返回 boolean 。如果你的if条件都不是 true 怎么办?在这种情况下,编译器将无法向方法的调用者返回任何内容。因此,无论条件是否满足,编译器都会告诉您为每种情况添加 return 语句。您应该在方法结束时添加 return false

Means your method should return the boolean value for every case. Currently it will return boolean if one of your if condition is true. What about if none of your if condition is true? In this case compiler will not be able to return anything to the caller of the method. So that compiler is telling you to add a return statement for method for every case no matter condition satisfy or not. You should add return false at the end of the method.

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

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