题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
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().toLowerCase();
int[] res = {0,0,0,0};
for(char b:str.toCharArray()){
if(b>='a'&&b<='z') res[0]++;
else if(b==' ') res[1]++;
else if(b>='0'&&b<='9')res[2]++;
else res[3]++;
}
for(int b:res){
System.out.println(b);
}
}
}
}
查看9道真题和解析