- package test;
- public class test {
- // 思路:将str转换为数组 遍历数组中的每个元素 若是字母则字母计数器加+ 下同
- public static void main(String[] args) {
- String s1 = "123aa %^09 ? ";
- char[] arr = s1.toCharArray();
- int acount = 0,spacecount = 0,digitalcount = 0,othercount=0;
- for (int i = 0; i < arr.length; i++) {
- if ((arr[i] >= 'a' && arr[i] <= 'z')||(arr[i] >= 'A' && arr[i] <= 'Z'))
- acount++;//ascii码表
- else if (arr[i] == ' ' )
- spacecount++;
- else if (arr[i] >= '0' && arr[i] <= '9')
- digitalcount++;
- else
- othercount++;
- }
- System.out.println("字母有:"+acount);
- System.out.println("数字有:"+digitalcount);
- System.out.println("空格有:"+spacecount);
- System.out.println("其他有:"+othercount);
- }
- }
复制代码 |