package demo1; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; import java.util.Set; /* * 计算一个字符串中每个字*/ public class DemoScannerCount { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入字符串:"); String str_sc = sc.next(); HashMap<Character,Integer> map = new HashMap<>(); for(char c :str_sc.toCharArray()){ if(map.containsKey(c)){ Integer value = map.get(c); value++; map.put(c,value); }else { map.put(c,1); } } Set<Character> set = map.keySet(); for (Character character : set) { System.out.println(character+"--->"+map.get(character)); } } }
Java 基础 计算字符串个数
最新推荐文章于 2021-11-20 14:12:31 发布