java - 运行此程序时出现无法访问的代码错误,但我不知道为什么

标签 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}
}

最佳答案

if(dx == 9);

虽然上面是一个有效的声明,代码应该用它来编译。但是接下来的语句 block :-

{
    System.out.println("Game Over");
    break;
}

的执行与任何条件无关。因此循环总能找到一个break。如果循环找到一个中断点,紧接着的下一条语句

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

无法访问。

关于java - 运行此程序时出现无法访问的代码错误,但我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966073/

相关文章:

java - 使用 APISpark reSTLet 扩展限制对 ReSTLet 资源的请求

java - 不同语言(即 Java 和 C++)的 "random"生成器如何比较?

javascript - 使用 javascript 将不同选择的选项存储到数组中

java - 根据内存和 GC 在循环内部或外部声明对象

bash - 使用wget使用bash脚本下载文件

java - 如何在 JTabbedPane 中聚焦当前打开的文档

java - 绝对 uri : http://java. sun.com/jsp/jSTL/core 无法在 web.xml 或随此应用程序部署的 jar 文件中解析

javascript - 创建具有附加属性和时刻的一系列工作日

javascript - 同一数组中的 javascript 对象之间的一对一关系 (AngularJS)

java - 为什么这个 Java for 循环终止,尽管它应该无限循环?