当前位置 : 主页 > 编程语言 > c++ >

java字符串统计字符

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt import java.util.HashMap;import java.util.Scanner;public class Chars {public static void main(String[] args) {Scanner scan = new Scanner(System.in);String inputStr = scan.nextLine();char[] chrs = inputStr.toCharArray();HashMap
gistfile1.txt
import java.util.HashMap;
import java.util.Scanner;

public class Chars {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String inputStr = scan.nextLine();
		char[] chrs = inputStr.toCharArray();
		HashMap map = new HashMap();
		for(char temp : chrs){
			if(map.containsKey(temp)){
				Integer v = (Integer)map.get(temp);
				map.put(temp, v+1);
			}else{
				map.put(temp, 1);
			}
		}
		System.out.println(map);
	}

}
网友评论