重复扫描仪输入问题?

时间:2013-12-17 02:45:28

标签: java java.util.scanner

我正在制作一个地牢探索型游戏,用户可以在每个回合中输入一个移动输入(选择单个移动方向)。但是,我遇到了扫描仪输入的问题。当我一次测试我的方法时它可以正常工作,但是当我把它放入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);
    }

}

1 个答案:

答案 0 :(得分:0)

您不应该关闭中心update方法

的scanLine对象
scanLine.close();