我不断收到“The operator == is undefined for the argument type(s) boolean, int”并且不知道如何解决它

发布于 2024-12-09 05:38:43 字数 1467 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖正好 2024-12-16 05:38:43

Java 与 C 不同,C 允许在某些情况下将变量视为不同类型有很大的自由度,但 Java 对允许执行的操作更为严格。您的两行有问题的行是:

private static boolean[] statesSaved = new boolean[7];
if (statesSaved[i] == 0)

您需要将最后一行重新编码为:

if (!statesSaved[i])

并且,请,如果您重视那些将维护您的代码的人的理智,请不要使用以下内容:

if (statesSaved[i] == false)

There should never任何需要显式比较布尔值的情况。您应该选择诸如 hasSavedStateisBroken 之类的智能名称,以便诸如:

if (isBroken)

或:之

while (!finished)

类的表达式在英语中有意义。这将大大增强代码的可读性。

无论如何,由于布尔值的显式比较会产生另一个布尔值,因此您会在哪里停止。历史悠久的反证法传统需要如下代码:

if (((finished == true) == true) == true) ...

Java, unlike C which allows a great deal of latitude in treating variables as differing types in certain circumstances, is a bit more strict in what you're allowed to do. Your two problematic lines are:

private static boolean[] statesSaved = new boolean[7];
if (statesSaved[i] == 0)

You need to recode that last one as:

if (!statesSaved[i])

And, please, if you value the sanity of those who will maintain your code, don't use things like:

if (statesSaved[i] == false)

There should never be any need to compare a boolean explicitly. You should instead choose intelligent names like hasSavedState or isBroken so that expressions like:

if (isBroken)

or:

while (!finished)

make sense in English. This will enhance the readability of your code greatly.

In any case, since the explicit comparison of a boolean results in another boolean, where would you stop. The time-honored tradition of reductio ad absurdum would require code like:

if (((finished == true) == true) == true) ...
只怪假的太真实 2024-12-16 05:38:43

正如错误消息所述,您的代码没有任何意义;你不能写if (boolean == int)

您的意思可能是 if (statesSaved[i] == false)

As the error message states, your code makes no sense; you cannot write if (boolean == int).

You probably meant if (statesSaved[i] == false)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文