I keep getting a “The operator == is undefined for the argument type(s) boolean, int” and have no idea how to fix it

让人想犯罪 __ 提交于 2019-11-28 14:21:26

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) ...

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!