我正在尝试将(字符串)数组与输入中的常规字符串进行比较。这可能吗?我有我的测试代码,它大部分只是说
incomparable types java.lang.string and java.lang.string[]
这是我的测试代码:
String[] yes = new String[5];
yes[0] = "yes";
yes[1] = "yeah";
yes[2] = "sure";
yes[3] = "yupp";
yes[4] = "okay";
System.out.println("Input");
String input = scan.next();
if ((input).equalsIgnoreCase(yes)) {
System.out.println("compared!");
}
最佳答案
最有效的解决方案是创建一组字符串,然后调用 set.contains 来查看输入字符串是否存在于该组中。示例代码:
HashSet<String> stringSet = new HashSet<String>();
//All string are already lower case so no need of toLowerCase() here
stringSet.add("yes");
stringSet.add("yeah");
stringSet.add("sure");
stringSet.add("yupp");
stringSet.add("okay");
System.out.println("Input");
String input = scan.next();
if (stringSet.contains(input.toLowerCase())) {
System.out.println("compared!");
}
关于java - 是否可以将数组字符串与普通字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347969/