手动输入可以工作。使用输入文件会导致无限循环?
我目前正在为学校做一项编程作业。这是一个简单的基于文本的角色扮演游戏。当我在本地(手动)测试该程序时,它工作正常。但是,当我将其提交到评分服务器时,它会创建某种无限循环。
我给我的教授发了电子邮件,他回复了我,解释了服务器如何测试程序。它使用以下格式:java IPA1(XML 文件名)< (输入文件)> (输出文件)
。 IPA1 是主 java 文件的名称。看来 < (输入文件)
由于某种原因导致无限循环......但我无法查明原因。
我的程序使用以下代码获取输入:
boolean gameOver = false;
while (!gameOver) {
Command cmd = inputParser.getCommand();
gameOver = processCommand(cmd);
}
getCommand(cmd)
如下:
public Command getCommand() {
String input = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
input = reader.readLine();
} catch(java.io.IOException exc) {
System.out.println ("Error");
}
return new Command(input);
}
我的问题是:为什么当我手动依次键入每个命令时,它会起作用......但当使用输入文件?
I'm currently working on a programming assignment for school. It's a simple text-based RPG. When I test the program locally (by hand), it works correctly. However, when I submit it to the grading server, it creates some sort of infinite loop.
I emailed my professor, who responded by explaining how the server tested the program. It uses the following format: java IPA1 (XML file name) < (Input file) > (output file)
. IPA1 is the name of the main java file. It seems that the < (Input file)
is causing the endless loop for some reason... but I cannot pinpoint why.
My program gets its input with the following code:
boolean gameOver = false;
while (!gameOver) {
Command cmd = inputParser.getCommand();
gameOver = processCommand(cmd);
}
The getCommand(cmd)
is as follows:
public Command getCommand() {
String input = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
input = reader.readLine();
} catch(java.io.IOException exc) {
System.out.println ("Error");
}
return new Command(input);
}
My question is: Why would this work when I type each command in sequentially by hand... but fail when an input file is used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您正在创建一个缓冲读取器,该读取器读取的内容超出了您请求的读取内容。当您告诉它读取五个字符时,它会读取更多字符并将其保存以供下一个请求使用,以提高性能。您应该重用一个 BufferedReader,而不是创建多个 BufferedReader,因为当您创建多个 BufferedReader 时,第一个 BufferedReader 已经获取了您想要与第二个 BufferedReader 一起读取的内容。
This is because you are creating a buffered reader that reads more than you requested it to read. When you tell it to read five characters, it reads a lot more and saves them for the next request to boost performance. You should reuse one BufferedReader instead of creating many of them because when you create many of them, the first one already grabs stuff you want to read with the second one.
尝试打印出您读到的内容 - 字符串末尾可能有新行符号或者文件有不同,因此它不会从循环中退出。您还可以调试程序以查看到底发生了什么。
如果您想知道为什么在这种情况下不起作用的确切答案,您需要发布
processCommand() 函数和文件的内容。
Try printing out what you read - there might be new line symbol at the end of the string or something is different about the file, so it doesn't exit from the loop. You can also debug the program to see exactly what's happening.
If you want exact answer to why in this case it doesn't work you need to post the
processCommand() function and the content of the file.