这是一个程序的一部分,它将计算文本文件中每个字母的出现次数。我希望它能打印出类似A:4 B:23 C:32的内容而不是它打印A:0B:0C:0A:0B:0C:0A:0B:0C:0甚至找不到每个出现的所有事件信件。我在这做错了什么?谢谢你的帮助!!
char ch = line.charAt(0);
int Acounter=0;
int Bcounter=0;
int Ccounter=0;
switch (ch)
{
case 'A':
Acounter++;
break;
case 'B':
Bcounter++;
break;
case 'C':
Ccounter++;
break;
}
bw.write ("A:" + Acounter);
bw.write ("B:" + Bcounter);
bw.write ("C:" + Ccounter);
答案 0 :(得分:0)
char ch [] = s.toCharArray(); Map map = new HashMap();
for (int i = 0; i < ch.length; i++) {
int count = 0;
for (int j = 0; j < ch.length; j++) {
if (ch[i] == ch[j])
count++;
}
map.put(ch[i], count);
}
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println("count of " + pairs.getKey() + " = "
+ pairs.getValue());
}