检查某事物是否等于java中的不同事物

检查某事物是否等于java中的不同事物,java,Java,例如: if(!UserInputSplit[i].equalsIgnoreCase("the" || "an") 生成sytax错误。解决这个问题的方法是什么?您需要显式地比较每个字符串 例如: if(!UserInputSplit[i].equalsIgnoreCase("the") || !UserInputSplit[i].equalsIgnoreCase("an")) 您需要显式地比较每个字符串 例如: if(!UserInputSplit[i].equalsIgnoreCase(

例如:

if(!UserInputSplit[i].equalsIgnoreCase("the" || "an")

生成sytax错误。解决这个问题的方法是什么?

您需要显式地比较每个字符串

例如:

if(!UserInputSplit[i].equalsIgnoreCase("the") || !UserInputSplit[i].equalsIgnoreCase("an"))

您需要显式地比较每个字符串

例如:

if(!UserInputSplit[i].equalsIgnoreCase("the") || !UserInputSplit[i].equalsIgnoreCase("an"))

如前面的回答所述,如果您有一小部分要比较的项目,那么使用一系列| |就可以了:

if (!UserInputSplit[i].equalsIgnoreCase("the") || !!UserInputSplit[i].equalsIgnoreCase("the")) {
  // Do something when neither are equal to the array element
}

但是,如果你有比一个小的项目集更大的东西,你可以考虑使用一个映射,或者一个集合:

// Key = animal, Value = my thoughts on said animal
Map<String, String> animals = new HashMap<String, String>();
animals.put("dog", "Fun to pet!");
animals.put("cat", "I fear for my life.");
animals.put("turtle", "I find them condescending.");

String[] userInputSplit = "I have one dog, a cat, and this turtle has a monocle.".split(" "); 

for (String word : UserInputSplit) {
  word = word.toLowerCase(); // Some words may be upper case. This only works if the cases match.
  String thought = animals.get(word);
  if (thought != null) {
    System.out.println(word + ": " + thought);
  }
}
//键=动物,值=我对所说动物的看法
映射动物=新HashMap();
动物。放(“狗”,“宠物的乐趣!”);
动物。放(“猫”,“我害怕我的生命。”);
动物。放(“海龟”,“我发现他们居高临下。”);
String[]userInputSplit=“我有一只狗,一只猫,这只乌龟有一个单片眼镜。”.split(“”);
for(字符串字:UserInputSplit){
word=word.toLowerCase();//有些单词可能是大写的。只有大小写匹配时才有效。
字符串思维=动物。获取(单词);
if(思想!=null){
System.out.println(word+“:”+思想);
}
}

如果采用这种方法,您当然希望将其放入自己的类中,或者以某种方式将其加载一次,因为您不希望每次都设置一个巨大的地图。

如果您有一小组要比较的项目,那么使用一系列| |就可以了,如前面的回答所述:

if (!UserInputSplit[i].equalsIgnoreCase("the") || !!UserInputSplit[i].equalsIgnoreCase("the")) {
  // Do something when neither are equal to the array element
}

但是,如果你有比一个小的项目集更大的东西,你可以考虑使用一个映射,或者一个集合:

// Key = animal, Value = my thoughts on said animal
Map<String, String> animals = new HashMap<String, String>();
animals.put("dog", "Fun to pet!");
animals.put("cat", "I fear for my life.");
animals.put("turtle", "I find them condescending.");

String[] userInputSplit = "I have one dog, a cat, and this turtle has a monocle.".split(" "); 

for (String word : UserInputSplit) {
  word = word.toLowerCase(); // Some words may be upper case. This only works if the cases match.
  String thought = animals.get(word);
  if (thought != null) {
    System.out.println(word + ": " + thought);
  }
}
//键=动物,值=我对所说动物的看法
映射动物=新HashMap();
动物。放(“狗”,“宠物的乐趣!”);
动物。放(“猫”,“我害怕我的生命。”);
动物。放(“海龟”,“我发现他们居高临下。”);
String[]userInputSplit=“我有一只狗,一只猫,这只乌龟有一个单片眼镜。”.split(“”);
for(字符串字:UserInputSplit){
word=word.toLowerCase();//有些单词可能是大写的。只有大小写匹配时才有效。
字符串思维=动物。获取(单词);
if(思想!=null){
System.out.println(word+“:”+思想);
}
}

如果采用这种方法,您当然希望将其放入自己的类中,或者以某种方式将其加载一次,因为您不希望每次都设置一个巨大的映射。

您只能对基本类型(即非字符串)进行
切换。@Th3Cuber不是真的!从Java7开始,您可以打开StringsOh,这是我的错误。谢谢大家!@Th3Cuber也不要忘记
enum
s。@Slanec-没错,但Java 7的字符串切换区分大小写,问题是不区分大小写的匹配。您只能
切换基元类型(即非字符串)。@Th3Cuber不是真的!从Java7开始,您可以打开StringsOh,这是我的错误。谢谢大家!@Th3Cuber也别忘了
enum
s。@Slanec-没错,但Java 7的字符串开关区分大小写,问题是关于不区分大小写的匹配。