非终止Java程序2
我遇到了与之前在此链接中发布的问题相同的问题。 我不知道为什么,但是当我再次尝试时,但这次使用 Eclipse(我在链接中使用了 Netbeans)。我得到的结果是完全错误的。在 UVAToolkit 中,误导性的空白可能会返回错误的答案。
如果有机会的话,我应该使用 BufferedReader 代替吗?
BufferedReader reader = new BufferedReader(InputStreamReader(System.in));
代码 [注:位于 main(String[] args) 方法中]
Scanner scanner = new Scanner(System.in);
String line;
while(!(line = scanner.nextLine()).equals("")) {
System.out.println(line);
}
System.out.println("done");
案例输入:[“1000”字符串末尾没有空格]
1 10
1 100
1 1000
案例输出 [注:“换行”属于空格输入]
1 10
1 100
"new-line"
1 1000
"new-line"
done
"new-line"
正确的大小写输出 [注意:“done”字符串末尾没有空格]
1 10
1 100
1 1000
done
提前致谢。
I have the same problem as before posted in this link.
I don't why but when I tried it again but this time using Eclipse (I used Netbeans in the link). I get this results which is strictly wrong. In UVAToolkit, ever misleading white-space could return a wrong answer.
If by any chance, should I use BufferedReader instead?
BufferedReader reader = new BufferedReader(InputStreamReader(System.in));
Code [Note: located in the main(String[] args) method]
Scanner scanner = new Scanner(System.in);
String line;
while(!(line = scanner.nextLine()).equals("")) {
System.out.println(line);
}
System.out.println("done");
Case Input: [No white-space at the end of "1000" string]
1 10
1 100
1 1000
Case Output [Note: "new-line" pertains to a white-space enter ]
1 10
1 100
"new-line"
1 1000
"new-line"
done
"new-line"
Correct Case Output [Note: No white-space at the end of the "done" string]
1 10
1 100
1 1000
done
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下代码将显示输入的结果字符串(如果它不是空字符串),并在完成时显示“done”。
关于您的期望如下:
我认为很难找到一种方法来避免空格(空字符串),这是循环的哨兵所需要的,除非最终输入是预期的为“1 1000”,我们可以按如下方式更改循环的哨兵:
Following code will display the result string of input if it is not an empty string and display "done" when it is done.
Regarding your expectation below:
I think it is quite difficult to find a way to avoid whitespace (empty string) which is needed as the sentinel to the loop, unless the final input is expected to be "1 1000" where we can change the sentinel of the loop as follows: