非终止Java程序
问题描述:
我遇到了与Java输入相关的问题.我正在UVAToolkit中解决某些情况,但是从 System.in 输入的行中存在一些问题.根据下面的这些代码,一旦按下键,如何解决该问题?示例输入/输出显示在下面.
I have this Java input-related problem. I'm solving some cases in UVAToolkit, but there are some problems that the line input requires from the System.in. Basing from these codes below, how could I terminate the problem once I've pressed key? The sample input/output are displayed below.
Scanner scanner = new Scanner(System.in);
String line;
while((line = scanner.nextLine()) != null) {
System.out.println(line);
}
System.out.println("done");
样本输入:
1 10
10 100
100 1000
样本输出:
1 10
10 100
100 1000
done
谢谢.
答
不要检查空输入,而是检查空字符串.这样,您只需按回车键就可以终止.
Don't check for null input but for an empty string. This way, you should be able to terminate just by pressing the return key.
while(!(line = scanner.nextLine()).equals(""))