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

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);
	}
}




你可能感兴趣的:(java例程练习(统计字母数字等字符的个数))