【发布时间】:2016-11-22 23:43:27
【问题描述】:
我需要从控制台获取多行输入,这些输入将是我的班级问题的整数。到目前为止,我一直在使用扫描仪,但我没有解决方案。输入由 n 行组成。输入以一个整数开头,然后是一系列整数,这会重复很多次。当用户输入 0 时,即停止输入。
例如
输入:
3
3 2 1
4
4 2 1 3
0
那么我如何阅读这一系列的行,并可能使用扫描仪对象将每一行存储为数组的一个元素?到目前为止,我已经尝试过:
Scanner scan = new Scanner(System.in);
//while(scan.nextInt() != 0)
int counter = 0;
String[] input = new String[10];
while(scan.nextInt() != 0)
{
input[counter] = scan.nextLine();
counter++;
}
System.out.println(Arrays.toString(input));
【问题讨论】:
-
你遇到了这个skipping issue
-
您需要 2 个循环:一个读取数量的外部循环和一个读取那么多整数的内部循环。在两个循环结束时,您需要
readLine()