我无法弄清楚如何允许输入包含用户想要的尽可能多的行。输入将至少包含 1 行。第一行将是一个整数,这个整数应该告诉程序之后有多少行,例如。
5
line1
line2
line3
line4
line5
我该怎么办?是否有一种类型的扫描仪允许这样做,或者我应该使用循环?
最佳答案
您不需要多个 Scanner 实例来处理此问题。只需使用一个带有循环的实例就足够了。
Scanner sc = new Scanner(System.in);
int nbLines = sc.nextInt();
sc.nextLine(); //consume the line separator token
for(int i = 0; i < nbLines; i++) {
String line = sc.nextLine();
//do something with the line
}
关于java - 如何使用多台扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28422697/