题解 | 判断各类型字符个数
判断各类型字符个数
https://www.nowcoder.com/practice/4ccc155e474e4d4c83cfde116dcf2218
import java.util.Scanner; public class Main { public static void main(String[] args) { int numbers = 0; int words = 0; int space = 0; int other = 0; Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); //write your code here...... int [] arr = getCharCount(str); words = arr[0]; numbers = arr[1]; space = arr[2]; other = arr[3]; System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other); } public static int[] getCharCount(String str){ int [] arr = new int [4]; char [] chars = str.toCharArray(); for(char c : chars){ if(c == ' '){ arr[2] ++; }else if('0' <= c && c <= '9'){ arr[1] ++; }else if('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'){ arr[0] ++; }else { arr[3] ++; } } return arr; } }