您的位置:首页 > 编程语言 > Java开发

笔试题——编写java程序:输入一个字符串,判断有几个英文字母,有几个数字,有几个其它的字符

2015-10-10 22:08 1016 查看
public static void main(String[] args) {

int count_abc=0,count_num=0,count_oth=0;
//输入一串数
Scanner scan=new Scanner(System.in);
String str = scan.next();
char[] chars = str.toCharArray();
//判断每个字符
for(int i = 0;i<chars.length;i++){
if((chars[i]>=65&&chars[i]<=90)||(chars[i]>=97&&chars[i]<=122)){
count_abc++;
}else if(chars[i]>=48&&chars[i]<=57){
count_num++;
}else{
count_oth++;
}
}
System.out.println("字母有:"+count_abc+"个");
System.out.println("数字有:"+count_num+"个");
System.out.println("其他的有:"+count_oth+"个");
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String getMess=scan.nextLine();
int zm=0;
int num=0;
int other=0;
char [] ch = getMess.toCharArray();
for(int i=0;i<ch.length;i++){
if((ch[i]>='a' && ch[i]<='z') || (ch[i]>='A' && ch[i]<='Z')){
zm=zm+1;
}else if(ch[i]>47 && ch[i]<58){
num=num+1;
}else{
other=other+1;
}
}
System.out.println("字母:"+zm);
System.out.println("数字:"+num);
System.out.println("其它:"+other);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: