解题思路: 先递归出每一个子字符串(一个),在进入正则表达式中进行判断
注意事项: 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 人评分
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:1435 |
钟神赛车 (C++代码)浏览:865 |
简单的a+b (C语言代码)浏览:530 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:520 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:459 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:522 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:594 |
a+b浏览:432 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:600 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:1479 |