我正在为一个程序编写代码,该程序显示基于x&文件的文件的地图。 y坐标,以及类型值。
我的代码正确显示了ASCII地图并允许我在地图上执行功能,但是当代码最终返回到switch语句时,它应该选择下一个选项的输入,它会跳过“option = input”行。 nextInt();”并继续到finally块“input.nextLine();”给了我一个Java.util.NoSuchElementException。
有谁知道这是为什么?我知道当没有这样的元素时,这个异常通常会发生枚举,但我不知道为什么我的代码会跳过input.nextInt()行然后在input.nextLine()上失败;
只有在最后的viewMap Switch案例中激活Gorgon案例时才会发生此错误。我尝试在“input.nextInt()”调用之前添加“input.nextLine()”调用,但它没有什么区别。
包括我的MapViewer程序中的Switch语句的代码,以及构建地图并显示它的代码。我最后还添加了Gorgon课程。
public class MapViewerMenu {
public int option = 0;
public boolean complete = false;
Scanner input = new Scanner(System.in);
char [][] mapper = null;
Map currentMap = null;
User currentUser = null;
Vector <Grue> currentGrues = null;
public void run(){
while(!complete){
System.out.println("*******************");
System.out.println("* Map Viewer Menu *");
System.out.println("*******************");
System.out.println("1. Load Files");
System.out.println("2. Set Symbols");
System.out.println("3. View Map");
System.out.println("4. Scramble Map");
System.out.println("5. Reset Map");
System.out.println("6. Exit");
System.out.println("");
try{
option = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again.");
System.out.println("");
}
finally{
// The Error occurs once the program exits viewMap and returns here.
//It skips the above "option = input.nextInt();" and comes down here and fails.
input.nextLine();
}
switch(option){
//removed irrelevant cases
case 3:
viewMap();
break;
case 6:
complete = true;
input.close();
break;
}
}
}
public void viewMap(){
//removed earlier code that built part of the map for readability
number = currentGrues.elements(); //this is an enumeration
while(number.hasMoreElements()){
temp3 = number.nextElement();
int sure =2;
switch(temp3.getName()){
case "Gorgon":
while(sure != 0){
System.out.printf("Would you like the Gorgon to change a square type?\n");
System.out.println("(0 for yes, 1 for no.)");
try{
sure = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again.");
System.out.println("");
sure = 2;
}
finally{
input.nextLine();
}
if(sure == 0){
Gorgon gor = (Gorgon) temp3;
gor.boulder(currentMap, mapper);
after = true;
break;
}
else{
if(sure == 1){
break;
}
}
}
break;
// A boolean value after tells the program to reprint the map.
if(after){
System.out.println("Since some squares were changed, the updated map is printed.");
System.out.println("");
System.out.printf("Map Name: %s", currentMap.getName());
System.out.println("");
for(xpos = 0, ypos = 0; xpos < 16 && ypos < 16;){
System.out.print(mapper[xpos][ypos]);
xpos++;
if(xpos < 16){
continue;
}
else{
System.out.println("");
ypos++;
if(ypos < 16){
xpos = 0;
continue;
}
}
}
System.out.println("");
}
}
//This should then go back to the "option = input.nextInt" line, where it should ask the user for input, but doesnt.
import java.util.Scanner;
public class Gorgon extends Giant {
//Included is the function called by the viewMap switch statement.
//I added this because the error might exist here.
//other classes have similar functions, but perform just fine.
public void boulder(Map map, char[][] mapper){
int x = 0;
int y = 0;
Scanner input = new Scanner(System.in);
System.out.println("The Gorgon wants to turn an adjacent Square to stone!");
System.out.printf("Its position is (%d,%d).\n", this.currPos.col, this.currPos.row);
do{
do{ // Get an adjacent x coordinate
try{
System.out.println("Enter an x coordinate:");
x = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again:");
System.out.println("");
}
finally{
input.nextLine();
}
}while(!(x >= currPos.col-1 && x <= currPos.col+1));
do{ // get an adjacent y coordinate
try{
System.out.println("Enter a y coordinate:");
y = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again:");
System.out.println("");
}
finally{
input.nextLine();
}
}while(!(y >= currPos.row-1 && y <= currPos.row+1)); // keep asking for input while the input isn't within range.
}while(!((x >= currPos.col-1 && x <= currPos.col+1) && (y >= currPos.row-1 && y <= currPos.row+1)));
mapper[x][y] = 'B'; //Change the value in map data.
input.close(); //Close local input reader.
用户在(3,3)
我收集的grues(6,6)中的Gorgon应该能够将相邻的方格变成“boulder”,这是用户无法忍受的障碍。
每当我选择使用Gorgon函数boulder时,我的程序在程序开始时到达原始switch语句时会失败。
程序运行时,通常会这样:
我加载了相应的文件(这些与错误无关。)
地图查看器菜单*
3
建筑地图......
然后会以ASCII字符显示地图。
用户的绳子被偷了!
你想让巨人改变方形吗?
(0表示是,1表示否。)
1
肉!肉!肉!
您希望Gorgon改变方形吗?
(0表示是,1表示否。)
0
Gorgon想把相邻的广场变成石头!
它的位置是(6,6)。
输入x坐标:
7
输入y坐标:
6
由于某些方块已更改,因此会打印更新的地图。
然后再次正确打印更新。
地图查看器菜单*
输入错误。 再试一次。
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at viewer.MapViewerMenu.run(MapViewerMenu.java:92) //points to the finally block at the beginning, where "input.nextLine()" is written.
at viewer.MapViewer.main(MapViewer.java:40) //This is just the original main that runs the program.
它返回到类的原始switch语句后失败,但它应该返回到开头并再次请求交换机的输入。
答案 0 :(得分:0)
通过进入我的Gorgon的巨石功能来修复错误,而不是在System.in上打开一个新的扫描仪,我只是在我的参数中传递了现有的扫描仪。
我在system.in上创建另一个扫描程序并关闭它的事实可能与我的其他扫描程序失败有关。
感谢所有人的帮助和时间。