我有4个字符串s1,s2,s3,s4.我想将它与"是","否"和"两者"进行比较.它必须是这样的(s1.equals("yes"));
怎么做这个比较?
我会存储这些字符串列表,并使用Collections工具来发现的频率yes和no.然后将您的条件应用于yes和的数量no.: -
List<String> list = new ArrayList<String>() {{
add("yes"); add("yes"); add("no"); add("no");
}};
int yes = Collections.frequency(list, "yes");
int no = Collections.frequency(list, "no");
if (yes == 4 || yes == 0) { // all "yes" or all "no"
System.out.println("Operation 1");
} else if (yes == 2) { // 2 "yes" and 2 "no"
System.out.println("Operation 2");
} else { // (1 "yes", 3 "no") or (1 "no", 3 "yes")
System.out.println("Operation 3");
}
Run Code Online (Sandbox Code Playgroud)
当然,我认为你的字符串只能是"yes"或"no".
| 归档时间: |
|
| 查看次数: |
2055 次 |
| 最近记录: |