这题主要难点在于使用双列集合存储数据,如果包含就要值加一,否则存一
public class Test1 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String line = sc.nextLine();
TreeMap<Character,Integer> tm = new TreeMap<>();
char[] chs = line.toCharArray();
for (char c : chs) {
tm.put(c, tm.containsKey(c)?tm.get(c)+1:1);
}
Set<Character> keySet = tm.keySet();
for (Character ch : keySet) {
System.out.print(ch+"("+tm.get(ch)+")");
}
}
}
|