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);
}