for(int j=0 ; j<str.length() ; j++) {
if(char[j]==(a||e||i||o||u))
count++;
}
我知道 (a||e||i||o||u) 的结果是一个布尔值,所以无法比较,但是我们如何检查是否存在多个字符?p>
I know the result of (a||e||i||o||u) is a Boolean so can't compare but how can we check for multiple character presence?
这不是你想要的.请使用堆栈 switch 语句:
This is not doing what you want. Please use a stack switch statement:
for(int j = 0; j < str.length(); j++)
switch(str.charAt(j)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
}
或者,由于我是正则表达式爱好者,这里有一个使用正则表达式的方法!:)
Or, since I'm a regex enthusiast, here's an approach using regular expressions! :)
Matcher matcher = Pattern.compile("[aeiou]").matcher(str);
while(matcher.find())
count++;
此代码中有一个错误,稍后修复,感谢用户2980077
这篇关于将一个字符与多个字符进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
“Char 不能被取消引用"错误quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch 语句 - 是“或"/“和"可能的?Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java替换字符串特定位置的字符?Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
具有 int 和 char 操作数的三元表达式的类型是什么What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
读取文本文件并存储出现的每个字符Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
为什么我需要在 byte 和 short 上显式转换 char 原语Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)