Java中如何提示用户输入一个文件名,显示该文件名中每个字母出现的次数,字母区分大小写
2条回答 默认 最新
关注
public static void count(String str){ //将字符串转化为字符数组 char[] chars = str.toCharArray(); //创建一个HashMap名为hm HashMap<Character,Integer> hm = new HashMap(); //定义一个字符串c,循环遍历遍历chars数组 for(char c : chars){ //containsKey(c),当c不存在于hm中 if(!hm.containsKey(c)){ hm.put(c,1); }else{ //否则获得c的值并且加1 hm.put(c, hm.get(c)+1); } } for(Character key: hm.keySet()){ //hm.keySet()代表所有键的集合,进行格式化输出 System.out.println(key + "====" + hm.get(key)); } } }
```
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 3无用