【发布时间】:2019-03-25 05:44:18
【问题描述】:
今天早些时候我问how to re-try/catch input mismatch exception without getting caught by infinite loop
但这是两个过程,首先游戏会询问用户网格的大小,然后在启动后它会要求他设置一个标志或跨过一个单元格(如果我的游戏将是否则它会打印出周围地雷的数量),但我得到了一些奇怪的错误 代码:
int gridSize = 0;
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("how much the size of the grid do you want");
while (!scanner.hasNextInt()) {
System.err.println("Try again, this time with a proper int");
scanner.next();
}
gridSize = scanner.nextInt();
}
MinesWeeper grid = new MinesWeeper(gridSize);
grid.printOut();
int choice = 0;
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("1-to step over a cell\n2-to set a flag on the cell");
while (!scanner.hasNextInt()) {
System.err.println("Try again, this time with a proper int");
scanner.next();
}
choice = scanner.nextInt();
}
boolean Continue = true;
while (Continue) {
switch (choice) {
case 1:
if (grid.chooseCell(1)) {
Continue = false;
}
break;
case 2:
grid.chooseCell(2);
break;
}
}
错误:
how much the size of the grid do you want
3
A B C
Try again, this time with a proper int
1 * * *
Exception in thread "main" java.util.NoSuchElementException
2 * * *
at java.util.Scanner.throwFor(Scanner.java:862)
3 * * *
1-to step over a cell
at java.util.Scanner.next(Scanner.java:1371)
at Array.Main.main(MinesWeeper.java:188)
2-to set a flag on the cell
奇怪的是它在我的打印语句之间打印异常消息(网格是一个语句,指令也是一个)
当我进行搜索时,我发现我不能在同一地点使用两台扫描仪, 但是如果它们在尝试使用资源时初始化,我该如何分离它们
【问题讨论】:
标签: java try-catch java.util.scanner try-with-resources