题解 | #统计字符#用系数统自带的函数判断字符属于哪一类
统计字符
http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
int letterNum = 0,whitespaceNum = 0,digitNum = 0,otherNum = 0;
String str = in.nextLine();
for(Character ch : str.toCharArray()){
if(Character.isLetter(ch)){
letterNum++;
}else if(Character.isDigit(ch)){
digitNum++;
}else if(Character.isWhitespace(ch)){
whitespaceNum++;
}else{
otherNum++;
}
}
System.out.println(letterNum);
System.out.println(whitespaceNum);
System.out.println(digitNum);
System.out.println(otherNum);
}
}
}