我有一个输入:
aN b c
a1 a2 a3 ... aN
例如:
4 3 2
2 1 2 1 (I have here 'a' numbers, a = 4)
5 6 3
3 9 5 7 3 (I have here 'a' numbers, a = 5)
0 0 0
当 a 或 b 或 c 等于 0 时,我想停止读取输入。我试过这个:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Test {
public static void main(String[] args) throws IOException {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
String line = "";
int a = -1, b = -1, c = -1;
LinkedList<Integer> list = new LinkedList<>();
while (a != 0 && b != 0 && c != 0)
{
line = in.readLine();
String tmp[] = line.split(" ");
a = Integer.parseInt(tmp[0]);
b = Integer.parseInt(tmp[1]);
c = Integer.parseInt(tmp[2]);
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
line = in.readLine();
list.clear();
tmp = line.split(" ");
for (int i = 0; i < tmp.length; i++) {
list.add(new Integer(Integer.valueOf(tmp[i])));
}
System.out.println("List = 4 3 2" + list);
}
}
}
但有了这个简单的输入:
4 3 2
2 1 2 1
5 6 3
3 9 5 7 3
0 0 0
即使我输入 3 个零,我的程序仍然在等待输入。如何改进它?
编辑:
你误会的人。我需要第二条读取线,因为我需要读取第二条(第四条,第六条)输入线...