题解 | #统计字符#--利用if分支
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String str = in.nextLine();
char[] chars = str.toCharArray();
int[] result = new int[4];
// 利用if分支
for (char chr : chars) {
if ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z')) {
result[0]++;
} else if (chr == ' ') {
result[1]++;
} else if (chr >= '0' && chr <= '9') {
result[2]++;
} else {
result[3]++;
}
}
Arrays.stream(result).forEach(System.out :: println);
}
}
}
