题解 | #统计字符# 真简单
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String regex1 = "[a-zA-Z]"; String regex2 = "[ ]"; String regex3 = "[0-9]"; String regex4 = "[^a-zA-Z0-9]"; int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; for (int i = 0; i < str.length(); i++) { String c = str.charAt(i) + ""; if (c.matches(regex1)) { count1++; } else if (c.matches(regex2)) { count2++; } else if (c.matches(regex3)) { count3++; } else if (c.matches(regex4)) { count4++; } } System.out.println(count1); System.out.println(count2); System.out.println(count3); System.out.println(count4); } }