import java.util.HashMap; import java.util.Scanner; import java.util.TreeMap; public class WorkNumber { /** * 统计输入单词出现的次数 * * @param args */ public static void main(String[] args) { // 新建输入对象 Scanner sn = new Scanner(System.in); String word = sn.nextLine(); // 新建TreeMap HashMap<String, Integer> tm = new HashMap<String, Integer>(); // 转化为数组 String[] sWord = word.split(" "); for (String s : sWord) { if (tm.containsKey(s)) { Integer i = tm.get(s) + 1; tm.put(s, i); } else { tm.put(s, 1); } } System.out.println(tm); } }
实验二作业