This method must return a result of type boolean(Java)(此方法必须返回 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;
我做错了什么?它给了我错误消息此方法必须返回布尔类型的结果".请帮忙.
What am I doing wrong? it gives me the error message "This method must return a result of type boolean". Please help.
推荐答案
此方法必须返回布尔类型的结果"
"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) 类型的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:此方法必须返回 boolean(Java) 类型的结果
基础教程推荐
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
