package com.itheima1;
import java.io.InputStreamReader;
import java.util.Map.Entry;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/*
* 8.输入一个字符串,分别统计出其中英文字母、空格、数字和其它字符的数量
* "What the hell that dog is doing here? That's mean!"
*/
public class Ninth_Count {
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(str);
// String str = new Scanner(System.in).next();
// System.out.println(str);
char[] ch = str.toCharArray();
TreeMap<Character, Integer> hm = new TreeMap<>();
for(char key : ch) {
if(!hm.containsKey(key)) {
hm.put(key, 1);
}else {
hm.put(key, hm.get(key)+1);
}
// hm.put(key, (!hm.containsKey(key) ? 1 : hm.get(key)+1));
}
//遍历集合
Set<Entry<Character, Integer>> entry = hm.entrySet();
Iterator<Entry<Character, Integer>> ite = entry.iterator();
while(ite.hasNext()) {
Entry<Character, Integer> entry2 = ite.next();
System.out.println(entry2);
}
}
}
|
|