如何在Java中比较两个以上的字符串?

Dro*_*ner 1 java string

我有4个字符串s1,s2,s3,s4.我想将它与"是","否"和"两者"进行比较.它必须是这样的(s1.equals("yes"));

  1. 如果所有字符串都等于"是",则应该给出一个结果.
  2. 如果所有字符串都等于"否",则应该给出一个结果.
  3. 如果任何2个字符串等于"是"而另外2个字符串等于"否"则必须给出一个结果.
  4. 如果任何3个字符串等于"是"且1个字符串等于"否",则必须给出一个结果.
  5. 如果任何3个stings等于"no"且1个字符串等于"yes",则必须给出一个结果..

怎么做这个比较?

Roh*_*ain 7

我会存储这些字符串列表,并使用Collections工具来发现的频率yesno.然后将您的条件应用于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".