【发布时间】:2016-06-15 09:04:37
【问题描述】:
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
我不知道我之前发过的好不好
【问题讨论】:
-
将 'u' 值打印到 while 循环中,并在按下 ENTER 时观察它是什么。相应地设置 if 和 inside if break,这对你有用。