输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
package com.java.LanqQiaoBei;
import java.util.Scanner;
public class LanLan {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Latter = 0, Blank = 0, Count = 0, Other = 0;
char[] a = null;
String s = sc.nextLine();
a = s.toCharArray();
for (int i = 0; i < a.length; i++) {
if ('0' <= a[i] && a[i] <= '9') {
Count++;
} else if (('a' <= a[i] && a[i] <= 'z') || ('A' <= a[i] && a[i] <= 'Z')) {
Latter++;
} else if (a[i] == ' ') {
Blank++;
} else {
Other++;
}
}
System.out.printf("数字:%d,字母:%d,空格:%d,其他:%d", Count, Latter, Blank, Other);
}
}
以下是代码的运行结果: