问题描述
- 刚刚学java不会,请大神帮忙解答
-
ufheasuhewauhjdfuiewhufujqWU8OIU9OQEOIGUE9IFFOPTEO9FQIFO9WR0WSQFFEFTEAQFRESFGAEFED
解决方案
public static void main(String[] args) {
String str = "It's the first day of class, and the room is buzzing with excitement and expectation,curiosity and uncertainty";
String[] strArrs = str.split(" ");
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < strArrs.length; i++) {
if (map.get(strArrs[i]) != null) {
Integer integer = map.get(strArrs[i]);
integer++;
map.put(strArrs[i], integer);
} else {
map.put(strArrs[i], 1);
}
}
int maxNum = 0;
String maxValue = null;
for (int i = 0; i < strArrs.length; i++) {
int tmp = map.get(strArrs[i]);
if(tmp > maxNum) {
maxNum = tmp;
maxValue = strArrs[i];
}
}
System.out.println(maxValue + " : " + maxNum);
}
解决方案二:
String msg = "It's the first day of class, and the room is buzzing with excitement and expectation,curiosity and uncertainty.";
String regex = "(\,|\.)";
msg = msg.replaceAll(regex, " ");
String[] arr = msg.split(" ");
Map<String,Integer> map = new HashMap<String,Integer>();
for (int i = 0; i < arr.length; i++) {
if (!map.containsKey(arr[i])) {
map.put(arr[i], 1);
}else{
int count = map.get(arr[i]) + 1;
map.put(arr[i], count);
}
}
System.out.println(map);
解决方案三:
这个问题主要考的是字符串对正则表达式的支持
解决方案四:
public static void main(String[] args) {
// "It's the first day of class, and the room is buzzing with excitement and expectation,curiosity and uncertainty"
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String str = sc.nextLine();
String[] strArrs = str.split(" ");
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < strArrs.length; i++) {
if (map.get(strArrs[i]) != null) {
Integer integer = map.get(strArrs[i]);
integer++;
map.put(strArrs[i], integer);
} else {
map.put(strArrs[i], 1);
}
}
int maxNum = 0;
String maxValue = null;
for (int i = 0; i < strArrs.length; i++) {
int tmp = map.get(strArrs[i]);
if (tmp > maxNum) {
maxNum = tmp;
maxValue = strArrs[i];
}
}
System.out.println(maxValue + " : " + maxNum);
}
时间: 2025-01-21 03:56:52