您的位置:首页 > 其它

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

2018-04-08 22:31 211 查看
import java.util.Scanner;

public class Exercise{
public static void main(String[] args) {
int digital=0;
int character=0;
int other=0;
int blank=0;
char[] ch=null;
Scanner scan= new Scanner(System.in);
String s=scan.nextLine();
ch=s.toCharArray();
for(int i=0;i<s.length();i++) {
char c=ch[i];
if(c>='0'&&c<='9') {
digital++;
}else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
character++;
}else if(c==' ') {
blank++;
}else {
other++;
}
}
System.out.println("数字个数:"+digital);
System.out.println("英文字母个数:"+character);
System.out.println("空格:"+blank);
System.out.println("其他字符个数:"+other);
}
}

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐