我用BufferedReader的ready()方法判断搞定的 public class Main { private static String regex = "\\s"; public static void main(String[] args) throws IOException { Set<String> set = new HashSet<String>(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < 50; i++) { if (in.ready()) { String text = in.readLine(); String[] arr = text.split(regex); for (String val : arr) { set.add(val); } } else { break; } } System.out.println(set.size()); in.close(); } }