题目:输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。
package text;
import java.util.Scanner;
public class test23 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String regex1="[a-zA-Z]";
int a=0;
String regex2=" ";
int b=0;
String regex3="[0-9]";
int c=0;
int d=0;
char[] arr=str.toCharArray();
for(int i=0;i<arr.length;i++) {
String str1=String.valueOf(arr[i]);
if(str1.matches(regex1)) {
a++;
}else if(str1.matches(regex2)) {
b++;
}else if(str1.matches(regex3)) {
c++;
}else {
d++;
}
}
System.out.println("英文字母的个数为:"+a+",空格的个数为:"+b+",数字的个数为:"+c+",其他字符的个数为:"+d);
}
}