package com.heima.test;
import java.util.Scanner;
import java.util.TreeMap;
public class Demo_Test1 {
/**
* 需求:统计输入的字符串中每个字符出现的次数
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
char[] arr = line.toCharArray();
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
for (char c : arr) {
if (!tm.containsKey(c)) {
tm.put(c, 1);
} else {
tm.put(c, tm.get(c) + 1);
}
}
for (Character c : tm.keySet()) {
System.out.print(c + "(" + tm.get(c) + ")");
}
}
}
|
|