随意给出一段英文短文作为输入,针对短文中各字符(包括标点符号)的统计其出现的频率值;
时间: 2024-03-14 19:45:29 浏览: 159
统计字符出现的次数
5星 · 资源好评率100%
Here is a short English text that you can use for character frequency analysis:
"The quick brown fox jumps over the lazy dog."
To perform character frequency analysis on this text in Java, you can use a HashMap to store the frequency of each character. Here is an example code snippet:
```java
String text = "The quick brown fox jumps over the lazy dog.";
HashMap<Character, Integer> freqMap = new HashMap<Character, Integer>();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (!freqMap.containsKey(c)) {
freqMap.put(c, 1);
} else {
freqMap.put(c, freqMap.get(c) + 1);
}
}
for (char c : freqMap.keySet()) {
System.out.println(c + ": " + freqMap.get(c));
}
```
This code first creates an empty HashMap to store the character frequencies. It then loops through each character in the text, adding it to the HashMap if it hasn't been seen before, or incrementing its frequency if it has. Finally, it loops through the HashMap and prints out the frequency of each character.
阅读全文