您的位置:首页 > 其它

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

2013-02-18 20:43 591 查看
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入字符串:");
String a = s.nextLine();
int abccount = 0;
int numcount = 0;
int spacecount = 0;
int othercount = 0;
char[] b = a.toCharArray();
for(int i = 0; i < b.length; i++){
if(b[i]>='a'&&b[i]<='z'||b[i]>='A'&&b[i]<='Z'){
abccount++;
}else if(b[i]>='0'&&b[i]<='9'){
numcount++;
}else if(b[i]==' '){
spacecount++;
}else{
othercount++;
}
}
System.out.println("字符串中含有的英文字母数为:" + abccount);
System.out.println("字符串中含有的数字数为:" + numcount);
System.out.println("字符串中含有的空格数为:" + spacecount);
System.out.println("字符串中含有的其他字符为:" + othercount);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐