题解 | 统计字符
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.HashMap;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String line = in.nextLine();
HashMap<Integer, Integer> map = new HashMap<>();
for (char c : line.toCharArray()) {
if (c == 32) {
map.put(2, map.getOrDefault(2, 0) + 1);
} else if (c >= 65 && c <= 90) {
map.put(1, map.getOrDefault(1, 0) + 1);
} else if (c >= 97 && c <= 122) {
map.put(1, map.getOrDefault(1, 0) + 1);
} else if (c >= 48 && c <= 57) {
map.put(3, map.getOrDefault(3, 0) + 1);
} else {
map.put(4, map.getOrDefault(4, 0) + 1);
}
}
System.out.println(map.getOrDefault(1, 0));
System.out.println(map.getOrDefault(2, 0));
System.out.println(map.getOrDefault(3, 0));
System.out.println(map.getOrDefault(4, 0));
}
}
}

