编写一下程序 从键盘上输入一个字符串,统计字符串中英文字母的个数及数字的个数。

如题所述

public class test {

public static void main(String[] args) {

System.out.println("请输入字符串:");
Scanner input = new java.util.Scanner(System.in);
String str= input.next();
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 +"字母个数:"+letter);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-12-16
不用那么复杂,汉字俩字节,英文一个字节,计算下关系就可以了

public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in, "GBK");
String input = s.nextLine();
byte[] inputBytes = input.getBytes();
System.out.println("英文字符数:" + (2* input.length() - inputBytes.length));
System.out.println("汉字字符数:" + (inputBytes.length - input.length()) );
}
}本回答被提问者采纳
第2个回答  2011-12-31
告诉你步骤吧。
1、拆分字符串,把字符串拆分成字符数组;
2、for循环做正则判断,要用到java的Pattern 类,每循环一次统计相应的个数;
3、没了
相似回答