[英]Issue taking repeated scanner input?
我正在制作一種地牢探索型游戲,用戶可以在每轉一圈(選擇一個方向移動)時輸入輸入。 但是,我在使用掃描儀輸入時遇到問題。 當我一次測試我的方法時,它可以正常工作,但是當我將其放入while循環中以便每次回合都可以從播放器中獲得新的動作時,在第一次輸入后出現錯誤。 我不知道為什么會收到此錯誤,因為它不應該每次運行時都在掃描新的輸入嗎? 感謝您的任何見解。
錯誤如下:
Enter your move: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at ActTwoGame.Update(ActTwoGame.java:63)
at ActTwoGame.main(ActTwoGame.java:42)
這是有問題的代碼:
import java.util.Scanner;
public class DungeonGame {
// Data to display the base geometry in the room
// When drawing on the screen the orientation will be flipped
// This is fine, since we'll fix it later
private static final int roomData[][] = {
{1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
private static int xPos = 1;
private static int yPos = 15;
// Tiles
private static final String WALL_GRAPHIC = "##";
private static final String BLANK_GRAPHIC = "..";
private static final String PLAYER_GRAPHIC = "!!";
public static String map = new String();
public static void main(String[] args)
{
Start();
while (true)
{
Draw();
Update();
}
}
public static void Start()
{
// Init logic
System.out.println("Hello! Welcome to Act Two: The Game.");
System.out.println("To control your player, use the WASD keys. You will be prompted for input, but you can");
System.out.println("only move one space per turn. Enter W for up, S for left, A for down, D for right.\n");
}
public static void Update() {
// Gameplay logic
String charMoves = "";
Scanner scanLine = new Scanner(System.in);
System.out.print("Enter your move: ");
charMoves = scanLine.next();
if (charMoves.equals("w") || charMoves.equals("W")) {
if (roomData[xPos-1][0] != 1)
xPos -= 1;
else
System.out.println("Ouch! That was a wall.");
}
else if (charMoves.equals("a") || charMoves.equals("A")) {
if (roomData[yPos-1][0] != 1)
yPos -= 1;
else
System.out.println("Ouch! That was a wall.");
}
else if (charMoves.equals("s") || charMoves.equals("S")) {
if(roomData[xPos+1][0] != 1)
xPos += 1;
else
System.out.println("Ouch! That was a wall.");
}
else if (charMoves.equals("d") || charMoves.equals("D")) {
if (roomData[yPos+1][0] != 1)
yPos += 1;
else
System.out.println("Ouch! That was a wall.");
}
else
System.out.println("Invalid move. No action was taken.");
scanLine.close();
}
public static void Draw() {
// Map drawing
for (int i = 0; i < roomData.length; i++) {
for (int j = 0; j < roomData[0].length; j++) {
if (roomData[i][j] == 1)
map += (WALL_GRAPHIC);
else if (i == yPos && j == xPos)
map += (PLAYER_GRAPHIC);
else
map += (BLANK_GRAPHIC);
}
map += "\n";
}
System.out.println(map);
}
}
您不應該關閉內部update
方法中的scanLine對象
scanLine.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.