Comparing a char with multiple characters(将一个字符与多个字符进行比较)
问题描述
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
这篇关于将一个字符与多个字符进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将一个字符与多个字符进行比较


基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01