简单的字符统 计工具

模拟word功能,实现显示光标所在的位置和用户选择文本所包含的字符数量

望采纳,谢谢

public class Bianli { public static void main(String[] args) { // 使用Scanner获取用户输入的字符串 Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); String str = sc.next();
    // 穿件Map集合,key是字符串中的字符,value是字符的个数
    HashMap<Character , Integer> map = new HashMap<>();

    // 遍历字符串,获取每一个字符
    for(char c : str.toCharArray()){
        if(map.containsKey(c)){
            // key存在
            Integer value = map.get(c);
            value++;
            map.put(c,value);
        }else{
            // key不存在
            map.put(c,1);
        }
    }

    // 遍历Map集合,输出结果
    for(Character key : map.keySet()){
        Integer value = map.get(key);
        System.out.println(key + "=" +value);
    }

}

}