package com.HashMap练习01;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class main {
public static void main(String[] args) {
HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
//可以使用TreeMap,元素有序
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char key = s.charAt(i);
Integer value = hm.get(key);
if (value == null){
hm.put(key,1);
}else{
value++;
hm.put(key,value);
}
}
StringBuilder stringBuilder = new StringBuilder();
Set<Character> keySet = hm.keySet();
for (Character key:keySet) {
Integer value = hm.get(key);
stringBuilder.append(key).append("(").append(value).append(")");
}
System.out.println(stringBuilder);
}
}
键盘录入一个字符串,统计字符串中每个字符串出现的次数(基础)(练习)
最新推荐文章于 2022-02-28 23:22:27 发布