Java编程题:输入英文以#结束,统计a-z的出现次数

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        Map<Character, Integer> m = new HashMap<>();
        for (int i = 97; i <= 122; i++) {
            m.put((char) i, 0);
        }
        while (input.hasNext()) {
            String s = input.nextLine().toLowerCase();
            for (int i = 0; i < s.toCharArray().length; i++) {
                if (m.containsKey(s.toCharArray()[i])) {
                    m.put(s.toCharArray()[i], m.get(s.toCharArray()[i]) + 1);
                }
            }
            if (s.contains("#")) {
                break;
            }
        }
        input.close();
        for (Map.Entry<Character, Integer> e : m.entrySet()) {
            System.out.println(e.getKey() + " " + e.getValue());
        }
    }
}

 

posted @ 2020-09-29 12:30  liw66  阅读(300)  评论(0编辑  收藏  举报