package apk; //输入一行字符,分别统计出其英文字母 空格 数字和其它字符的个数 import java.util.Scanner; public class I { public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); System.out.println("input:"); String str = sc.nextLine(); int charNum = 0,spaceNum = 0,num = 0, otherNum = 0; int sum = str.length(); char[] arr = str.toCharArray(); for(int i = 0; i < sum; i++) { if((arr[i] < 'z' && arr[i] > 'a') || (arr[i] > 'A' && arr[i] < 'Z')) charNum++; else if(arr[i] > '0' && arr[i] < '9') num++; else if(arr[i] == ' ') spaceNum++; else otherNum++; } System.out.println("charNum:" + charNum); System.out.println("spaceNum:" + spaceNum); System.out.println("num:" + num); System.out.println("otherNum:" + otherNum); } } 运行结果: