案例题:计算一个字符串中每个字符出现的次数

案例分析图:

 代码:

复制代码
复制代码
  public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个字符串");
        String next = sc.nextLine();

        char[] chars = next.toCharArray();
        Arrays.sort(chars);

        HashMap<Character, Integer> map = new HashMap<>();
        for (char aChar : chars) {
            if (map.containsKey(aChar)) {
                Integer value = map.get(aChar);
                value++;
                map.put(aChar,value);
            }else{
                map.put(aChar,1);
            }
        }
        System.out.println(map);
    }
复制代码
复制代码
 
posted @   想见玺1面  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示