不是很难,可以根据字符特性来判断
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.nextLine(); // 定义需要的结构 int eng = 0; int space = 0; int num = 0; int ch = 0; for (int i = 0; i < str.length(); i++) { char nowChar = str.charAt(i); if (nowChar >= 'a' && nowChar <= 'z' || nowChar >= 'A' && nowChar <= 'Z') { eng++; } else if (nowChar == ' ') { space++; } else if (nowChar >= '0' && nowChar <= '9') { num++; } else { ch++; } } System.out.println(eng); System.out.println(space); System.out.println(num); System.out.println(ch); } } }