简体   繁体   English

我如何使用多台扫描仪

[英]How can i use multiple scanners

I am having trouble figuring out how I can allow input that will consist of as many lines as the user wants. 我在弄清楚如何允许输入包含用户希望的多行内容时遇到了麻烦。 Input will consists of at least 1 line. 输入将至少包含1行。 On the first line will be an integer, this integer is suppose to tell the program how many lines are going to follow after, for ex. 第一行将是一个整数,例如,该整数告诉程序将要跟随多少行。

    5
    line1
    line2 
    line3
    line4
    line5

What should I do? 我该怎么办? Is there a type of scanner that will allow this or should i use loops? 是否有允许使用的扫描仪类型?还是应该使用循环?

You don't need multiple Scanner instances to handle this. 您不需要多个Scanner实例来处理。 Just use one instance with a loop is sufficient. 只需将一个实例与一个循环一起使用就足够了。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM