私信TA

用户名:uq_31143795822

访问量:2717

签 名:

等  级
排  名 6227
经  验 1382
参赛次数 0
文章发表 6
年  龄 19
在职情况 学生
学  校
专  业

  自我简介:

在校的一位菜鸡大学生

解题思路: 先递归出每一个子字符串(一个),在进入正则表达式中进行判断

注意事项: Scanner类中 next()方法和nextLine()方法的一个区别是next不读取空格,nextLine读取空格,我就是因为这点出错

参考代码:

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
     int arr[] = {0,0,0,0};
     String str = sc.nextLine();//可以读取空格
     statistics(str,arr,0);
     for (int i = 0; i < arr.length; i++) {
         if (i<arr.length-1) {
           System.out.print(arr[i]+" ");
       }else {
         System.out.print(arr[i]);
       }
     }
    }
    
    //递归
    public static void statistics(String str,int arr[],int index) {
        if (index==str.length()) { //出口
            return ;
        }
        
        judge(str.charAt(index),arr); //判断每一个字符串
        statistics(str,arr,++index); //开始递归
    }
    
    public static void judge(char s,int arr[]) {
        String str = String.valueOf(s);
        //判断字母
        String letter = "^[a-zA-Z]{1}";
        if (str.matches(letter)) {
            arr[0]++;
        }
        
        //判断数字
        String number = "^[0-9]{1}";
        if (str.matches(number)) {
            arr[1]++;
        }
        
        //判断空格
        String space = "\\s{1}";
        if (str.matches(space)) {
            arr[2]++;
        }
        
        //判断其他的字符
        String other = "^[^a-zA-Z0-9\\s]{1}";
        if (str.matches(other)) {
            arr[3]++;
        }
    }


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区