java 我不断收到“运算符 == 未定义参数类型布尔型,int”并且不知道如何修复它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7733982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I keep getting a "The operator == is undefined for the argument type(s) boolean, int" and have no idea how to fix it
提问by Bernhard Zwahlen
I keep getting a "The operator == is undefined for the argument type(s) boolean, int" in this bit of code at line 3:
在第 3 行的这段代码中,我不断收到“运算符 == 未定义参数类型布尔型,int”的信息:
public void loadState(int i)
{
if (statesSaved[i] == 0)
{
return;
}
List list = TMIUtils.getMinecraft().h.at.e;
for (int j = 0; j < 44; j++)
{
sx slot = (sx)list.get(j + 1);
slot.c(null);
ul itemstack = TMIUtils.copyStack(states[i][j]);
if ((itemstack == null) || (itemstack.c < 0) || (itemstack.c >= sv.f.length) || (sv.f[itemstack.c] == null))
continue;
slot.c(itemstack);
}
}
I have no idea why this happens, since i have another class file with the same thing where there is no error.
我不知道为什么会发生这种情况,因为我有另一个具有相同内容的类文件,其中没有错误。
Here is my full class file if it helps:
如果有帮助,这是我的完整类文件:
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.client.Minecraft;
public class TMIConfig
{
public static final String VERSION = "1.7.2 2011-07-01";
public static final int NUM_SAVES = 7;
public static final int INVENTORY_SulE = 44;
public static boolean isModloaderEnabled = false;
private static TMIConfig instance;
private static List items = new ArrayList();
private static HashSet toolIds;
private static HashSet nonUnlimitedIds;
private Map settings;
private static ul[][] states = new ul[7][44];
private static boolean[] statesSaved = new boolean[7];
private static HashSet excludeIds;
public TMIConfig()
{
this.settings = new LinkedHashMap();
this.settings.put("enable", "true");
this.settings.put("enablemp", "false");
this.settings.put("give-command", "/give {0} {1} {2}");
for (int i = 0; i < getNumSaves(); i++)
{
this.settings.put(new StringBuilder().append("save-name").append(i + 1).toString(), new StringBuilder().append("").append(i + 1).toString());
}
for (int j = 0; j < getNumSaves(); j++)
{
this.settings.put(new StringBuilder().append("save").append(j + 1).toString(), "");
}
instance = this;
}
public static boolean isMultiplayer()
{
return TMIUtils.getMinecraft().f.I;
}
public static TMIConfig getInstance()
{
if (instance == null)
{
new TMIConfig();
}
return instance;
}
public Map getSettings()
{
return this.settings;
}
public List getItems()
{
return items;
}
public int getNumSaves()
{
return 7;
}
public boolean isStateSaved(int i)
{
return statesSaved[i];
}
public ul[] getState(int i)
{
return states[i];
}
public boolean getBooleanSetting(String s)
{
return Boolean.parseBoolean((String)this.settings.get(s));
}
public boolean isEnabled()
{
return ((isMultiplayer()) && (getBooleanSetting("enablemp"))) || ((!isMultiplayer()) && (getBooleanSetting("enable")));
}
public void toggleEnabled()
{
String s = isMultiplayer() ? "enablemp" : "enable";
this.settings.put(s, Boolean.toString(!getBooleanSetting(s)));
}
public void setEnabled(boolean flag)
{
String s = isMultiplayer() ? "enablemp" : "enable";
this.settings.put(s, Boolean.toString(flag));
}
public static boolean isItemIncluded(int i)
{
return !excludeIds.contains(Integer.valueOf(i));
}
public static boolean isTool(sv item)
{
return toolIds.contains(Integer.valueOf(item.br));
}
public static boolean canItemBeUnlimited(sv item)
{
return !nonUnlimitedIds.contains(Integer.valueOf(item.br));
}
public boolean areDamageVariantsShown()
{
if (isMultiplayer())
{
String s = (String)getSettings().get("give-command");
return s.contains("{3}");
}
return true;
}
public void clearState(int i)
{
for (int j = 0; j < 44; j++)
{
states[i][j] = null;
statesSaved[i] = false;
}
this.settings.put(new StringBuilder().append("save").append(i + 1).toString(), "");
}
public void loadState(int i)
{
if (statesSaved[i] == 0)
{
return;
}
List list = TMIUtils.getMinecraft().h.at.e;
for (int j = 0; j < 44; j++)
{
sx slot = (sx)list.get(j + 1);
slot.c(null);
ul itemstack = TMIUtils.copyStack(states[i][j]);
if ((itemstack == null) || (itemstack.c < 0) || (itemstack.c >= sv.f.length) || (sv.f[itemstack.c] == null))
continue;
slot.c(itemstack);
}
}
public void saveState(int i)
{
List list = TMIUtils.getMinecraft().h.at.e;
for (int j = 0; j < 44; j++)
{
states[i][j] = TMIUtils.copyStack(((sx)list.get(j + 1)).a());
}
this.settings.put(new StringBuilder().append("save").append(i + 1).toString(), encodeState(i));
statesSaved[i] = true;
}
public String encodeState(int i)
{
StringBuilder stringbuilder = new StringBuilder();
for (int j = 0; j < 44; j++)
{
if (states[i][j] != null)
{
stringbuilder.append(states[i][j].c);
stringbuilder.append(":");
stringbuilder.append(states[i][j].a);
stringbuilder.append(":");
stringbuilder.append(states[i][j].i());
}
stringbuilder.append(",");
}
return stringbuilder.toString();
}
public void decodeState(int i, String s)
{
if (s.trim().equals(""))
{
statesSaved[i] = false;
}
else {
String[] as = s.split(",", 0);
for (int j = 0; (j < as.length) && (j < states[i].length); j++)
{
String[] as1 = as[j].split(":");
if (as1.length != 3)
{
continue;
}
try
{
states[i][j] = new ul(Integer.parseInt(as1[0]), Integer.parseInt(as1[1]), Integer.parseInt(as1[2]));
}
catch (Exception exception)
{
System.out.println(exception);
}
}
statesSaved[i] = true;
}
}
static
{
toolIds = new HashSet();
for (int i = 1; i <= 3; i++)
{
toolIds.add(Integer.valueOf(i + 256));
}
for (int j = 11; j <= 23; j++)
{
toolIds.add(Integer.valueOf(j + 256));
}
for (int k = 27; k <= 30; k++)
{
toolIds.add(Integer.valueOf(k + 256));
}
for (int l = 34; l <= 38; l++)
{
toolIds.add(Integer.valueOf(l + 256));
}
for (int m = 42; m <= 61; m++)
{
toolIds.add(Integer.valueOf(m + 256));
}
toolIds.add(Integer.valueOf(359));
toolIds.add(Integer.valueOf(346));
nonUnlimitedIds = new HashSet();
nonUnlimitedIds.add(Integer.valueOf(358));
excludeIds = new HashSet();
excludeIds.add(Integer.valueOf(9));
excludeIds.add(Integer.valueOf(11));
excludeIds.add(Integer.valueOf(63));
excludeIds.add(Integer.valueOf(64));
excludeIds.add(Integer.valueOf(68));
excludeIds.add(Integer.valueOf(71));
excludeIds.add(Integer.valueOf(74));
excludeIds.add(Integer.valueOf(75));
excludeIds.add(Integer.valueOf(59));
excludeIds.add(Integer.valueOf(83));
excludeIds.add(Integer.valueOf(55));
excludeIds.add(Integer.valueOf(26));
excludeIds.add(Integer.valueOf(93));
excludeIds.add(Integer.valueOf(94));
try
{
Class.forName("mod_RedPowerWiring");
excludeIds.add((Integer)ModLoader.getPrivateValue(mod_RedPowerWiring.class, null, "InsWireID"));
excludeIds.add((Integer)ModLoader.getPrivateValue(mod_RedPowerWiring.class, null, "CableID"));
}
catch (Exception exception)
{
}
}
}
回答by paxdiablo
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:
Java 与 C 不同,C 允许在某些情况下将变量视为不同类型有很大的自由度,Java 对您可以做的事情更加严格。您的两条有问题的线路是:
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 neverbe any need to compare a boolean explicitly. You should instead choose intelligent names like hasSavedState
or isBroken
so that expressions like:
应该永远是任何需要比较明确的一个布尔值。你应该选择像hasSavedState
orisBroken
这样的智能名称,这样的表达式如下:
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 anotherboolean, where would you stop. The time-honored tradition of reductio ad absurdum would require code like:
在任何情况下,由于布尔值的显式比较导致另一个布尔值,你会在哪里停止。由来已久的减少广告荒谬的传统需要如下代码:
if (((finished == true) == true) == true) ...
回答by SLaks
As the error message states, your code makes no sense; you cannot write if (boolean == int)
.
正如错误消息所述,您的代码毫无意义;你不能写if (boolean == int)
。
You probably meant if (statesSaved[i] == false)
你可能是说 if (statesSaved[i] == false)