题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.Scanner; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNextLine()){ int englishCount = 0; int namespaceCount = 0; int numberCount = 0; int otherCount = 0; String inputStr = scanner.nextLine(); char[] array = inputStr.toCharArray(); Pattern pattern1 = Pattern.compile("[a-z|A-Z]"); Pattern pattern2 = Pattern.compile("[0-9]"); for(char temp : array){ Matcher matcher1 = pattern1.matcher(String.valueOf(temp)); if(matcher1.find()){ englishCount++; continue; } Matcher matcher2 = pattern2.matcher(String.valueOf(temp)); if(matcher2.find()){ numberCount++; continue; } if (" ".equals(String.valueOf(temp))){ namespaceCount++; continue; } otherCount++; } System.out.println(englishCount); System.out.println(namespaceCount); System.out.println(numberCount); System.out.println(otherCount); } } }