edit:it was 我的一个错误,错误地改变了测试输入。它现在像一个魅力:)谢谢你们的帮助!如果你想的话,你可以结束这个问题。
我有一个任务,应该读取用户输入(测试文件)并返回一些数据(输出)。一切都运行良好,我通过了我的测试2,但由于一些愚蠢的错误,在我的结束流关闭(通过测试后)。
main处理用户输入并声明和运行scanner,它调用我创建的类中的方法(由于赋值限制,我无法更改main)。
这是我在课堂上写的代码:
public class MyInstance extends Intersection {
private boolean methodHasRun = false;
// keeps count of getIntersectionState() loop
private static int loop_count;
public String getIntersectionState(){
String left = "LVg0";
String right = "RVg0";
String bottom = "BVr0";
String top = "TVr0";
String temp = " ";
String temp1 = " ";
String temp2 = " ";
String temp3 = " ";
String nr [] = {"1","2","3","4","5","6","7","8","9","10"};
// if advanceTime() was executed
if (this.methodHasRun) {
//get last char, write it to temp
temp = bottom.substring(bottom.length()-1).trim();
temp1 = top.substring(top.length()-1).trim();
while(loop_count < nr.length-1) {
temp = "BVr" + nr[loop_count];
temp1 = "TVr" + nr[loop_count];
++loop_count;
// break after each increment
break;
}
if(loop_count == 9) {
temp = "BVr" + nr[8];
temp1 = "TVr" + nr[8];
++loop_count;
}else if(loop_count > 9) {
while(loop_count < 20) {
temp = "BVg" + nr[8];
temp1 = "TVg" + nr[8];
temp2 = "LVr" + nr[loop_count-10];
temp3 = "RVr" + nr[loop_count-10];
++loop_count;
// can't save left and right out of else if
// because they will be assigned to: " "
left = temp2;
right = temp3;
break;
}
}
if(loop_count == 21) {
temp = "BVg" + nr[9];
temp1 = "TVg" + nr[9];
temp2 = "LVr" + nr[9];
temp3 = "RVr" + nr[9];
left = temp2;
right = temp3;
}
while(loop_count == 20) {
temp2 = "LVr" + nr[9];
temp3 = "RVr" + nr[9];
++loop_count;
break;
}
if (loop_count > 21) {
System.out.println("go on");
}
bottom = temp;
top = temp1;
// System.out.println("State changed!"+
// "\nLoop count is: "+loop_count);
// revert to false until state changes again
this.methodHasRun = false;
} else {
System.out.println("State hasn't changed");
}
return left +" "+ right +" "+ bottom +" "+ top;
}
}
它在运行时生成:
测试输入文件是:
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
应输出:
LVg0 RVg0 BVr0 TVr0
LVg0 RVg0 BVr1 TVr1
LVg0 RVg0 BVr2 TVr2
LVg0 RVg0 BVr3 TVr3
LVg0 RVg0 BVr4 TVr4
LVg0 RVg0 BVr5 TVr5
LVg0 RVg0 BVr6 TVr6
LVg0 RVg0 BVr7 TVr7
LVg0 RVg0 BVr8 TVr8
LVg0 RVg0 BVr9 TVr9
LVr1 RVr1 BVg9 TVg9
LVr2 RVr2 BVg9 TVg9
LVr3 RVr3 BVg9 TVg9
LVr4 RVr4 BVg9 TVg9
LVr5 RVr5 BVg9 TVg9
LVr6 RVr6 BVg9 TVg9
LVr7 RVr7 BVg9 TVg9
LVr8 RVr8 BVg9 TVg9
LVr9 RVr9 BVg9 TVg9
LVr10 RVr10 BVg9 TVg9
LVg10 RVg10 BVr10 TVr10
我希望有人能理解我的错误或提出一些建议。
1条答案
按热度按时间ylamdve61#
你粘贴的每一行内容都与你的问题完全无关。相关部分是您使用和关闭扫描仪的位置,以及引发“cannot parse command”异常的位置。
几点注意事项
如果你打电话来
scanner.close()
找个地方或者用资源把它 Package 起来,不要这样做。如果你不能修复代码,找一个能修复的人,告诉他们去修复他们的bug。如果你必须解决它,你可以覆盖它
System.in
使用忽略close()
电话。这是有点复杂,是围绕着一个错误-即大大低于只修复你的错误,而不是工作。如果没有这个或删除scanner.close()/尝试使用资源,就没有什么魔力了--do-not-close-scanners
选项。如果堆栈跟踪是由扫描仪关闭引起的,则异常处理将中断。你至少应该这样做
throw new IllegalArgumentException("Cannot parse command", e);
-那个, e
部分原因。这是好的,因为你现在还没有这么做,这使得你不可能知道到底出了什么问题(我只需要看看你的标题,它显然是一个封闭的系统。