/*请输入字符串,判断每个字符出现次数*/
public class mapTest {
public static void main(String[] args) {
System.out.println("请输入字符串,判断每个字符出现次数");
String s =new Scanner(System.in).next() ;
HashMap<Character,Integer> map= new HashMap<Character, Integer>();
for(int i=0;i<s.length();i++){
if(!map.containsKey(s.charAt(i))){
map.put(s.charAt(i), 1);
//count++;
}
else {
int count = map.get(s.charAt(i));
count++;
map.put(s.charAt(i), count);
}
}
System.out.println(map);
Set<Character> keySet = map.keySet();
for (Character k : keySet) {
int count = map.get(k);
System.out.println("字符"+k+"出现的次数 "+count );
}
}
}
|
|