Java编程题之统计一行字符中的英文字母、空格、数字和其它字符的个数。

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

直接进主题----->写代码

		int digital = 0;
		int character = 0;
		int other = 0;
		int blank = 0;
		char[] ch = null;
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		ch = s.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] >= '0' && ch[i] <= '9') {
				digital++;
			} else if ((ch[i] >= 'a' && ch[i] <= 'z') || ch[i] >= 'A' && ch[i] <= 'Z') {
				character++;
			} else if (ch[i] == ' ') {
				blank++;
			} else {
				other++;
			}
		}
		System.out.println("数字个数: " + digital);
		System.out.println("英文字母个数: " + character);
		System.out.println("空格个数: " + blank);
		System.out.println("其他字符个数:" + other);

版权声明:本文为weixin_46503529原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。