import java.util.*;
public class WorldCount {
public static void main(String[] args) {
Map map = new HashMap<String, Integer>();
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("please input:");
String word = sc.nextLine();
if (map.containsKey(word)) {
Integer value = (Integer) map.get(word);
value++;
map.put(word, value);
} else {
map.put(word, 1);
}
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = (Integer) map.get(key);
System.out.print(key + " " + value + " \n");
}
}
}
}
此篇博客介绍了一个Java程序,它通过Scanner读取用户的输入,不断更新并显示每个单词出现的次数。核心是使用HashMap存储词频,并遍历展示结果。适合学习数据流处理和基础数据结构的实现。
875

被折叠的 条评论
为什么被折叠?



