使用JAVA语句编程 题目:利用对话框读入字符串,统计输入字符串中数字字符和英文字母的个数。在线等答案 谢

如题所述

String str="sd 12 332 sdk ;;; ~! dfj3";
System.out.println(str.length());
int num=0;//数字
int letter=0;//字母,包括大写和小写
int space=0;//空格
int other=0;//其他
for(int i=0;i<str.length();i++){
char c=str.charAt(i);
int value=(int)c;
if(value==32){
++space;
}else if(value>=48 && value<=57){
++num;
}else if((value>=65 && value<=90) || (value>=97 && value<=122) ){
++letter;
}else{
++other;
}
}

System.out.println("数字个数:"+num+"\n字母个数:"+letter+"\n空格个数:"+space+"\n其他字符个数:"+other);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-25
evf