为什么说方法的返回值从未被使用?
尝试自己制作井字游戏。制定此方法是为了检查是否交叉获胜。 但它说返回值从未使用过。 checkField[9]的sout是这样的 {0, 1, 2 3, 4, 5 6, 7, 8} 因此,如果你赢了,它会返回 true,如果你没有赢,它会返回 false。 但事实并非如此。
static boolean isCrossWin(String[] checkField) {
if (
checkField[0] == "X" && checkField[4] == "X" && checkField[8] == "X" ||
checkField[3] == "X" && checkField[4] == "X" && checkField[5] == "X" ||
checkField[6] == "X" && checkField[4] == "X" && checkField[2] == "X" ||
checkField[7] == "X" && checkField[4] == "X" && checkField[1] == "X" ||
checkField[0] == "X" && checkField[3] == "X" && checkField[6] == "X" ||
checkField[6] == "X" && checkField[7] == "X" && checkField[8] == "X" ||
checkField[8] == "X" && checkField[5] == "X" && checkField[2] == "X" ||
checkField[0] == "X" && checkField[1] == "X" && checkField[2] == "X"
) {
return true;
} else {
return false;
}
}
Trying to make TicTacToe by myself. Making this method in order to check is Crosses win.
But it says that returned value never used.
sout of checkField[9] is like this
{0, 1, 2
3, 4, 5
6, 7, 8}
So it shall return true if you win and false if you don't.
But it doesn't.
static boolean isCrossWin(String[] checkField) {
if (
checkField[0] == "X" && checkField[4] == "X" && checkField[8] == "X" ||
checkField[3] == "X" && checkField[4] == "X" && checkField[5] == "X" ||
checkField[6] == "X" && checkField[4] == "X" && checkField[2] == "X" ||
checkField[7] == "X" && checkField[4] == "X" && checkField[1] == "X" ||
checkField[0] == "X" && checkField[3] == "X" && checkField[6] == "X" ||
checkField[6] == "X" && checkField[7] == "X" && checkField[8] == "X" ||
checkField[8] == "X" && checkField[5] == "X" && checkField[2] == "X" ||
checkField[0] == "X" && checkField[1] == "X" && checkField[2] == "X"
) {
return true;
} else {
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码有两件事需要担心。第一个,也是我认为更相关的,是处理检查字符串的方式。在 Java 中,要比较两个字符串,必须使用
String#equals()
而不是==
。要解决这个问题,您必须仔细检查并更改每一个
checkField[x] == "X"
到checkField[x].equals("X");
到目前为止,这对您来说可能不是问题(在某些情况下 == 仍然有效,但它们是实现的意外),您必须解决这个问题。
接下来,只要不使用结果,您就需要查看如何在代码的其余部分中实际应用该方法。您可以使用一些简单的调试技巧来查看实际发生的情况,即使用 调试器或只需在两种返回情况下添加打印语句即可。无论哪种方式,如果该函数实际上没有返回某些内容,那么这绝对是您找出原因的最佳猜测。
There are two things to worry about with this code. The first, and more pertinent in my opinion, is dealing with the way you are checking the strings. In Java to compare two strings you MUST use
String#equals()
and not==
.To fix this you would have to go through and change every single
checkField[x] == "X"
tocheckField[x].equals("X");
This may not have been an issue up to this point for you (there are cases where == will still work, but they are accidents of the implementation), you must fix this.
Next, as far as not using the result goes, you need to look at how you are actually applying the method in the rest of your code. Some simple debugging tips you could use to see what is actually happening is either using a debugger or by simply adding print statements in both of the return cases. Either way, if the function actually isn't returning something, that would definitely be your best guess for finding out why.