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


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM