java程序用常见类统计所给字符所含的大写字母,小写字母,空格,数��

如题所述

public class test {
public static void main(String[] args) {
//要统计的字符串
String s = "n290ur #@%8 ujj29rj10xk9" ;
//大写字母个数
int a = 0;
//小写字母个数
int b = 0 ;
//数字个数
int c = 0;
//空格个数
int d = 0;
//其他字符个数
int e = 0;

String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s2 = "abcdefghijklmnopqrstuvwxyz";
String s3 = "0123456789";
String s4 = " ";

for(int i= 0;i < s.length();i++){
char str = s.charAt(i);
if(s1.indexOf(str)!=-1){
a++;
}else if(s2.indexOf(str)!=-1){
b++;
}else if(s3.indexOf(str)!=-1){
c++;
}else if(s4.indexOf(str)!=-1){
d++;
}else{
e++;
}
}
System.out.println("大写字母个数:"+a);
System.out.println("小写字母个数:"+b);
System.out.println("数字个数:"+c);
System.out.println("空格个数:"+d);
System.out.println("其他字符个数:"+e);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-04
这么简单的程序都不会写,你还是早点放弃编程吧!
相似回答