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.hasNext()) { // 注意 while 处理多个 case
String str = in.nextLine();
int element = 0;
int speace = 0;
int num = 0;
int other = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z' || str.charAt(i) >= 'a' &&
str.charAt(i) <= 'z') {
element++;
} else if (str.charAt(i) == ' ') {
speace++;
} else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
num++;
} else {
other++;
}
}
System.out.println(element);
System.out.println(speace);
System.out.println(num);
System.out.println(other);
}
}
}
忘记了Z和a之间差了几个字符,所以用这种方法就需要大小写的边界分清。
System.out.println('a'-0);
System.out.println('Z'-0);
97
90

京公网安备 11010502036488号