我用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();
}
}
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashSet<String> set = new HashSet<String>();
while (in.hasNext()) {//注意while处理多个case
set.add(in.next());
}
System.out.println(set.size());
}
}