public class LetterAndNumberCount { public static void main(String[] args) { Count("FJJgjsgfsd543632"); count1("SFsefgdg2354354fsdf"); } public static void Count(String str){ int A = 0; int a = 0; int Number = 0; byte[] bytes = str.getBytes(); // 将字符串转为byte数组
for(int i=0;i<bytes.length;i++){ if(bytes[i] >= 'a' && bytes[i] <= 'z'){ a++; }else if(bytes[i]>='A' && bytes[i]<='Z'){ A++; }else if(bytes[i]>='0' && bytes[i]<='9'){ Number++; } } System.out.println("大写字母个数为:"+ A); System.out.println("小写字母个数为:"+ a); System.out.println("数字个数为:"+ Number); } public static void count1(String str){ int A = 0; int a = 0; int Number = 0; for(int i=0;i<str.length();i++){ char array = str.charAt(i); //便利数组后用charAt(i)来返回指定字符然后if进行判断
if(array >= 'a' && array <= 'z'){ a++; }else if(array>='A' && array<='Z'){ A++; }else if(array>='0' && array<='9'){ Number++; } } System.out.println("大写字母个数为:"+ A); System.out.println("小写字母个数为:"+ a); System.out.println("数字个数为:"+ Number); } }