把非大写字母全部清除,剩下的字母个数就是大写字母的个数 public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String a = in.nextLine().replaceAll("[^A-Z]", ""); System.out.println(a.length()); } }