[英]how to compare more than two Strings in Java?
I have 4 strings s1,s2,s3,s4. 我有4个字符串s1,s2,s3,s4。 and i want to compare it with "yes","no" and "both" It has to be like
(s1.equals("yes"));
我想将其与“是”,“否”和“两者”进行比较,它必须像
(s1.equals("yes"));
How to do this comparison? 怎么做比较?
I would store those strings in a list, and use Collections
utility to find the frequencies of yes
and no
. 我会将这些字符串存储在列表中,然后使用
Collections
实用程序查找yes
和no
的频率。 Then apply your conditions to number of yes
and 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");
}
Of course, I assume that your strings can only be "yes"
or "no"
. 当然,我假设您的字符串只能是
"yes"
或"no"
。
You can try this logic. 您可以尝试这种逻辑。 This one does what you intend to do :
这是您打算做的事情:
if(s1.equals("yes") && s2.equals("yes") && s3.equals("yes") && s4.equals("yes"))
result1;
else if (s1.equals("no") && s2.equals("no") && s3.equals("no") && s4.equals("no"))
result2;
else if ((s1.equals("yes") || s2.equals("yes")) && (s3.equals("no") || s4.equals("no")))
result3;
else if((s1.equals("yes") || s2.equals("yes") || s3.equals("yes")) && s4.equals("no"))
result4;
else if((s1.equals("no") || s2.equals("no") || s3.equals("no")) && s4.equals("yes"))
result5;
Assuming they are either "yes" or "no", use this: 假设它们是“是”或“否”,请使用以下命令:
int yesCount = (s1+s2+s3+s4).replace("no", "").length() / 3;
Then base your logic on that, possibly with a switch(yesCount)
然后基于此逻辑,可能需要使用
switch(yesCount)
String[] str={s1,s2,s3,s4};
int yesCount=0, noCount=0;
for(int i=0;i<str.length();i++){
if("yes".equals(str[i]))
yescount++;
else if("no".equals(str[i]))
noCount++;
}
String check=""+yesCount+noCount;
switch(check){
case "40":
//do whatever
break;
case "30":
//do whatever
break;
case "20":
//do whatever
break;
case "10":
//do whatever
break;
case "31":
//do whatever
break;
case "21":
//do whatever
break;
case "11":
//do whatever
break;
case "22":
//do whatever
break;
case "12":
//do whatever
break;
case "13":
//do whatever
break;
case "04":
//do whatever
break;
case "00":
//do whatever
break;
}
Assuming string value can be either yes or no 假设字符串值可以是yes或no
int result = 0;
if("yes".equalsIgnoreCase(s0)) result++;
if("yes".equalsIgnoreCase(s1)) result++;
if("yes".equalsIgnoreCase(s2)) result++;
if("yes".equalsIgnoreCase(s3)) result++;
switch(result){
case 0:
System.out.println("all strings are NO");
break;
case 1:
System.out.println("3 strings are NO, 1 string is YES");
break;
case 2:
System.out.println("2 strings are NO, 2 strings are YES");
break;
case 3:
System.out.println("1 string is NO, 3 strings are YES");
break;
case 4:
System.out.println("all strings are YES");
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.