Java程序检查字符串中每个元音的出现
要再次计算字符串中元音的出现次数,请使用java的Map实用工具计算字符串中每个字符的出现次数。将每个元音放在Map中作为键,并将每个键的初始值设置为1,现在将每个字符与map的键进行比较如果字符与键匹配,则将其对应的值增加1。
示例
public class OccurenceVowel {
public static void main(String[] args) {
String str = "AEAIOG";
LinkedHashMap<Character, Integer> hMap = new LinkedHashMap();
hMap.put('A', 0);
hMap.put('E', 0);
hMap.put('I', 0);
hMap.put('O', 0);
hMap.put('U', 0);
for (int i = 0; i <= str.length() - 1; i++) {
if (hMap.containsKey(str.charAt(i))) {
int count = hMap.get(str.charAt(i));
hMap.put(str.charAt(i), ++count);
}
}
System.out.println(hMap);
}
}输出结果
{A=2, E=1, I=1, O=1, U=0}热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短