题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//97-122 a-z 65-90 A-Z
//32 空格
//48-57 0-9
//其他
String str=in.nextLine();
int[] ary=new int[4];//创建int型空数组所有值默认=0
char ch;
for(int i=0;i<str.length();i++){
ch=str.charAt(i);
if((ch>=65 && ch<=90)|| (ch>=97 && ch<=122)){
ary[0]++;
}else if(ch==32){
ary[1]++;
}else if(ch>=48 && ch<=57){
ary[2]++;
}else{
ary[3]++;
}
}
for(int res:ary){
System.out.println(res);
}
}
}
枚举法: 依次枚举出每一个字符的ascll码值,判断属于哪项就放哪里
本题只有4组,所有int[4]完全没问题
