题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5?tpId=37&tqId=21263&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fdifficulty%3D2%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=2&judgeStatus=undefined&tags=&title=
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); char[] chars = str.toCharArray(); int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; for(int i = 0;i < chars.length;i++){ if(((int) chars[i] >= 65 && (int)chars[i] <= 90) || ((int) chars[i] >= 97 && (int)chars[i] <= 122)){ count1++; }else if(chars[i] == ' '){ count2++; }else if((int)chars[i] >=48 && (int)chars[i] <= 57){ count3++; }else{ count4++; } } System.out.println(count1); System.out.println(count2); System.out.println(count3); System.out.println(count4); } }
利用字符对应的ASCII即可求解