java对于键盘输入的一段英文,请统计这段话中所含英文单词数并输出每个单词。 英文: Academic expert retrieval is a process of searching recognized researchers with high academic status and influence in a given scientific research field, relying on computer as a tool and using statistical knowledge, data mining and artificial intelligence technologies.代码
时间: 2023-05-18 08:06:03 浏览: 114
从键盘上输入一串英文字符(不含空格与其他字符),统计每个字符的数目,并输出字母及相应的数目。
以下是统计英文单词数并输出每个单词的 Java 代码:
import java.util.*;
public class WordCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一段英文:");
String input = scanner.nextLine();
String[] words = input.split("[^a-zA-Z]+");
Map<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
if (!word.isEmpty()) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
}
System.out.println("单词数:" + wordCount.size());
System.out.println("每个单词出现的次数:");
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
注意:这段代码只能统计英文单词,对于中文或其它语言的单词无法处理。
阅读全文