用java 代码写一个找出字母最多的程序
时间: 2023-05-18 15:05:51 浏览: 69
以下是一个用 Java 代码写的找出字符串中字母最多的程序:
```java
import java.util.HashMap;
import java.util.Map;
public class MaxLetterCount {
public static void main(String[] args) {
String str = "Hello World";
Map<Character, Integer> map = new HashMap<>();
int maxCount = 0;
char maxChar = ' ';
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isLetter(c)) {
int count = map.getOrDefault(c, 0) + 1;
map.put(c, count);
if (count > maxCount) {
maxCount = count;
maxChar = c;
}
}
}
System.out.println("The letter that appears the most is '" + maxChar + "' with a count of " + maxCount);
}
}
```
这个程序会遍历字符串中的每个字符,如果是字母就统计出现次数,并记录出现次数最多的字母和出现次数。最后输出结果。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)