借助HashMap统计每个字符出现的次数


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine().toLowerCase();
        char ch = in.next().charAt(0);//读取一个char字符
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0) + 1);
        }
        System.out.println(map.getOrDefault(Character.toLowerCase(ch), 0));
    }
}