非终止Java程序 [英] Non-terminating Java program

查看:56
本文介绍了非终止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(""))

这篇关于非终止Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆