题解 |HJ40 #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
// HJ40 统计字符
// 统计其中英文字符,空格字符,数字字符,其他字符的个数
// 输入:1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
char[] chars = str.toLowerCase().toCharArray();
int numAlphebat = 0;
int space = 0;
int number = 0;
int other = 0;
for (int i = 0; i < chars.length; i++) {
if (48 <= chars[i] && chars[i] <= 57) {
number++;
} else if (97 <= chars[i] && chars[i] <= 122) {
numAlphebat++;
} else if (chars[i] == ' ') {
space++;
} else {
other++;
}
}
// 英文字符,空格字符,数字字符,其他字符
System.out.println(numAlphebat);
System.out.println(space);
System.out.println(number);
System.out.println(other);
}
}
查看8道真题和解析