我运行时遇到无法访问的代码错误,但我不知道原因

时间:2017-10-27 01:17:02

标签: java arrays loops compiler-errors unreachable-code

我有一个任务,我需要创建一个游戏,让你在10x10板周围移动一块。你转移到" ^"空间,你赢了。这是我到目前为止的代码,但我遇到了问题。我在代码中暂停一下,当玩家进入9时它假设退出游戏。但是当中断时,我得到一个无法访问的代码错误。如果我把它拿出来,游戏就可以正常运行,除非它在每次输入后都说游戏结束,你不能像你一样退出游戏。我把它放在错误的位置吗?或者,如果玩家进入9,他们有更好的方法让游戏退出吗?

import java.util.Scanner;
import java.util.Random;

public class MineWalker {
    public static final int BOARD_SIZE = 10;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int pX = 0;
        int pY = 0;
        Random r = new Random();
        int gX = r.nextInt(BOARD_SIZE);
        int gY = r.nextInt(BOARD_SIZE);

        Spaces[][] board = new Spaces[BOARD_SIZE][BOARD_SIZE];

        for (int y = 0; y < board.length; y++) //clears board
        {
            for (int x = 0; x < board[y].length; x++) {
                board[x][y] = Spaces.Empty;
            }
        }
        board[pX][pY] = Spaces.Player;
        board[gX][gY] = Spaces.Goal;

        System.out.println("Welcome to Mine Walker. Get the ice cream cone and avoid the mines");

        boolean gameOver = false;
        while (gameOver == false) //game loop
        {
            for (int y = 0; y < board.length; y++) //Draws board
            {
                for (int x = 0; x < board[y].length; x++) {
                    switch (board[x][y]) {
                        case Empty:
                            System.out.print("_");
                            break;
                        case Player:
                            System.out.print("X");
                            break;
                        case Goal:
                            System.out.print("^");
                            break;
                    }
                }
                System.out.println();
            }
            System.out.println("Enter either a -1, 0, or 1 in the X or 9 to quit");//moves game piece
            int dx = keyboard.nextInt();
            if (dx == 9) ;
            {
                System.out.println("Game Over");
                break;
            }
            System.out.println("Enter either a -1,0, or 1 in the Y"); // Unreachable statement here
            int dy = keyboard.nextInt();

            if (dx < -1 || dx > 1) {
                System.out.println("Invalid x");
                dx = 0;
            }
            if (dy < -1 || dy > 1) {
                System.out.println("Invalid y");
                dy = 0;
            }
            board[pX][pY] = Spaces.Empty;

            pX += dx;
            pY += dy;

            if (board[pX][pY] == Spaces.Goal) {
                System.out.println("You win!");
                gameOver = true;
            }
            board[pX][pY] = Spaces.Player;
        }
    }

    ;

    enum Spaces {Empty, Player, Goal}
}

1 个答案:

答案 0 :(得分:3)

if(dx == 9);

虽然上面是一个有效的语句,代码应该用这个编译。但接下来的一系列陈述: -

{
    System.out.println("Game Over");
    break;
}
无论任何条件,都执行

。因此循环总是找到break。如果循环找到了中断,那么下一个语句

System.out.println("Enter either a -1,0, or 1 in the Y");

无法访问。