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

public static void main(String[] args) {
//输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
String str="ABab哈哈 123,";
int letter=0;//字母
int space=0;//空格
int number=0;//数字
int other=0;//其他
for (int i = 0; i < str.length(); i++) {
char ch=str.charAt(i);//从字符串中获取字符
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
letter++;
}else if(ch==' '){//空格
space++;
}else if(ch>='0'&&ch<='9'){//数字
number++;
}else {
other++;
}
}
System.out.println("英文字母的个数是:"+letter+",空格的个数是:"
+space+",数字的个数是:"+number+",其他的个数是:"+other);
}

posted @ 2018-08-11 12:36  Captain灬  阅读(1717)  评论(0编辑  收藏  举报