如何统计某字符串中某个字符的数量

2025-04-13 02:54:01
推荐回答(1个)
回答1:

private static void ma2() {
Scanner scanner = new Scanner(System.in);
System.out.println("输入内容..");
String s = scanner.nextLine();
Map map = new HashMap<>();
for(int i = 0;iif(!map.containsKey(s.charAt(i))){
map.put(s.charAt(i), 1);
}else{
map.put(s.charAt(i), map.get(s.charAt(i))+1);
}
}
Set> entry = map.entrySet();
for(Entry e : entry){
System.out.println("值:"+e.getKey()+"--次数:"+e.getValue());
}
}