import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter a string :");
String line = in.nextLine();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
Integer integer = map.get(ch);
if (integer == null) {
map.put(ch, 1);
} else {
map.put(ch, integer + 1);
}
}
int length = line.length();
while (length > 0) {
for (Character c : map.keySet()) {
Integer count = map.get(c);
if (count == length) {
System.out.println("'" + c + "' : " + map.get(c));
}
}
length--;
}
}
}
输出:
Please enter a string :
hello world! hello java!
'l' : 5
' ' : 3
'o' : 3
'!' : 2
'a' : 2
'e' : 2
'h' : 2
'r' : 1
'd' : 1
'v' : 1
'w' : 1
'j' : 1
追问谢谢
追答哎呀呀,他回答的早是真的,不过他没有实现按出现频率大小顺序输出啊。
追问你们都是大神
追答原来是网友采纳,我错怪题主了。