首页 > 其他 > 详细

代码实现:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

时间:2017-03-09 20:37:59      阅读:205      评论:0      收藏:0      [点我收藏+]
import java.util.Scanner;
import java.util.TreeMap;

//输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
public class Test {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一行字符:");
		String s = sc.nextLine();
		char[] c = s.toCharArray();
		TreeMap<Character, Integer> tm = new TreeMap<>();
		for (char d : c) {
			if (!tm.containsKey(d)) {
				tm.put(d, 1);
			} else {
				tm.put(d, tm.get(d) + 1);
			}
		}
		for (Character ca : tm.keySet()) {
			System.out.println(ca + "(" + tm.get(ca) + ")");
		}
	}

}

 

代码实现:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

原文:http://www.cnblogs.com/loaderman/p/6527341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!