问题描述
我得到了以下代码,但在第一次循环之后,我的调试器发出以下错误,很奇怪,我一直这样做,但它不再起作用,这很奇怪?!
我一步一步检查它,它只是在切换顺序后停止?
线程中的异常 mainjava.lang.IllegalStateException:Scanner在java.util.Scanner.ensureOpen(未知来源)关闭
在java.util.Scanner.findWithinHorizon(未知来源)
at java.util .Scanner.nextLine(未知来源)
在Level.schleife(Level.java:35)
在Crawler.main(Crawler.java:23)
public boolean schleife(){
System.out .println(Das Spiel beginnt,beichge Dich mit der WASD Steuerung!);
Scanner eingabeMove = new Scanner(System.in);
tmpi = positioni;
tmpj = positionj;
while(true){
String bewegung = eingabeMove.nextLine();
switch(bewegung){
casew:{//vorwärts
tmpi + = 1;
if(actionResult()){
positioni = tmpi;
休息;
} else {
返回false;
}
}
案例a:{//链接
tmpj - = 1;
if(actionResult()){
positionj = tmpj;
休息;
} else {
返回false;
}
}
cases:{//rückwärts
tmpi - = 1;
if(actionResult()){
positioni = tmpi;
休息;
} else {
返回false;
}
}
案例d:{// rechts
tmpj + = 1;
if(actionResult()){
positionj = tmpj;
休息;
} else {
返回false;
}
}
默认值:{// falsche Eingabe
System.out.println(Falsche Eingabe!);
继续;
}
}
eingabeMove.close();
}
}
Don在结束时调用
循环。您导致扫描程序在第一个循环结束时无法运行。 eingabeMove.close();
,而
由于循环始终以 return $ c结束$ c>,在这个
schleife()
方法中关闭扫描仪是没有意义的。
你实际上不需要关闭扫描仪,因为它包装 System.in
,它永远不会关闭。鉴于这一事实,当 schleife()
返回时,您可以简单地让 eingabeMove
超出范围。
如果你真的想要关闭扫描仪,你应该将 eingabeMove
作为参数传递给方法,并从调用中关闭它使用扫描仪
}
致电代码:
扫描仪eingabeMove =新扫描仪(System.in);
schleife(eingabeMove);
eingabeMove.close();
I got the following code, but after the first loop, my debugger is giving following errors,it s strange, i did it all the time like that, but it doesnt work anymore, this is pretty strange?!I checked it step-by-step, it is just stoppen after the switch order?
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.findWithinHorizon(Unknown Source)
at java.util.Scanner.nextLine(Unknown Source)
at Level.schleife(Level.java:35)
at Crawler.main(Crawler.java:23)
public boolean schleife() {
System.out.println("Das Spiel beginnt, bewege Dich mit der WASD Steuerung!");
Scanner eingabeMove = new Scanner(System.in);
tmpi = positioni;
tmpj = positionj;
while (true) {
String bewegung = eingabeMove.nextLine();
switch (bewegung) {
case "w": { // vorwärts
tmpi += 1;
if (actionResult()) {
positioni = tmpi;
break;
} else {
return false;
}
}
case "a": { // links
tmpj -= 1;
if (actionResult()) {
positionj = tmpj;
break;
} else {
return false;
}
}
case "s": { // rückwärts
tmpi -= 1;
if (actionResult()) {
positioni = tmpi;
break;
} else {
return false;
}
}
case "d": { // rechts
tmpj += 1;
if (actionResult()) {
positionj = tmpj;
break;
} else {
return false;
}
}
default: { // falsche Eingabe
System.out.println("Falsche Eingabe!");
continue;
}
}
eingabeMove.close();
}
}
Don't call eingabeMove.close();
at the end of that while
loop. You're causing the Scanner to become inoperable at the end of the first loop.
Since the loop always terminates with a return
, it doesn't make sense to close the Scanner in this schleife()
method.
You actually don't need to close the Scanner though, because it wraps System.in
which never closes anyhow. Given this fact, you can simply let eingabeMove
go out of scope when the schleife()
returns.
If you really want to close the Scanner, you should pass eingabeMove
as a parameter to the method, and close it from the calling method.
public boolean schleife(Scanner eingabeMove) {
// use the scanner
}
Calling code:
Scanner eingabeMove = new Scanner(System.in);
schleife(eingabeMove);
eingabeMove.close();
这篇关于java.lang.IllegalStateException:扫描程序已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!