停止键盘输入?

Stop keyboard enter?

Scanner s = new Scanner(System.in);
List<Integer> solutions = new LinkedList<>();
int o = 0;

while (o != 10) {        // I want to read 2 numbers from keyboard   
    int p = s.nextInt(); // until send and enter, this is where is my
    int c = s.nextInt(); //doubt
    int d = p + c;
    solutions.add(d);  
    o = System.in.read();
}

Iterator<Integer> solution = solutions.iterator();
while (solution.hasNext()) {
    int u = solution.next();
    System.out.println(u);
}

我遇到的问题是,如何发送结束循环的输入?因为如果我再输入 2 个数字,System.in.read() 会取第一个数字,例如,

条目:

2 3(输入)读出2个数求和

1 2(输入)读出2个数求和

(enter) 在这里结束循环,因为没有输入数字,给出了解决方案

退出:

5

3

我不知道我以前发过什么

读入整行并自行解析。如果该行为空,则退出循环结束程序。如果它不为空,则将该行传递给新的扫描仪。

List<Integer> solutions = new LinkedList<>();

Scanner systemScanner = new Scanner(System.in);
String line = null;

while ((line = systemScanner.nextLine()) != null && !line.isEmpty()) {
    Scanner lineScanner = new Scanner(line);
    int p = lineScanner.nextInt();
    int c = lineScanner.nextInt();
    int d = p + c;
    solutions.add(d);
}

Iterator<Integer> solution = solutions.iterator();
while (solution.hasNext()) {
    int u = solution.next();
    System.out.println(u);
}