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

java例程练习(统计字母数字等字符的个数)

2012-04-30 14:17 357 查看
public class Test {
public static void main(String[] args) {
//String s = "48WERSFas!@#";
String s = "23479odurqjPOWUER00*)*&)(#084234-9LRWEJRLJ5R2)*q#)*puFOURoupPU_(*";

int countNum = 0;
int countUpperCase = 0;
int countLowerCase = 0;
int countOther = 0;
char[] sc = new char[s.length()];

for(int i = 0; i < s.length(); i++) {
sc[i] = s.charAt(i);

if(sc[i] >= 65 && sc[i] <= 90) {
countUpperCase ++;
} else if(sc[i] >= 87 && sc[i] <= 122) {
countLowerCase ++;
} else if(sc[i] >= 48 && sc[i] <= 57){
countNum ++;
} else {
countOther ++;
}
}

System.out.println(countNum);
System.out.println(countUpperCase);
System.out.println(countLowerCase);
System.out.println(countOther);
}
}
//还有一种方法
public class Test {public static void main(String[] args) {String s = "48WERSFas!@#";//String s = "23479odurqjPOWUER00*)*&)(#084234-9LRWEJRLJ5R2)*q#)*puFOURoupPU_(*";int countNum = 0;int countUpperCase = 0;int countLowerCase = 0;int countOther = 0;for(int i = 0; i < s.length(); i++) {char c = s.charAt(i);if(c >= 'A' && c <= 'Z') {countUpperCase ++;} else if(c >= 'a' && c <= 'z') {countLowerCase ++;} else if(c >= '0' && c <= '9'){countNum ++;} else {countOther ++;}}System.out.println(countNum);System.out.println(countUpperCase);System.out.println(countLowerCase);System.out.println(countOther);}}

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