我在while循环中遇到麻烦

时间:2017-05-20 18:57:47

标签: java

我正在用Java制作一个简单的“Whack a mole”游戏。为简单起见,我创建了一个10 * 10的盒子,并在随机盒子中放置10个痣。当用户花费他的50个输入或发现所有10个摩尔时,我想退出游戏,但是即使用户尝试指定的输入,似乎仍然存在终止while循环的问题。

是实例变量范围问题吗?为什么不工作?

public class WhackAMole {

    int score = 0, molesLeft = 10, attempts;
    char[][] moleGrid = new char[10][10];
    int numAttempts, gridDimension;

    public WhackAMole(int numAttempts, int gridDimension) {
        // TODO Auto-generated constructor stub
        this.numAttempts = numAttempts;
        this.gridDimension = gridDimension;
    }

    boolean place(int x, int y) {
        return (x == 2 && y == 5) 
            || (x == 1 && y == 3) 
            || (x == 8 && y == 4) 
            || (x == 5 && y == 10) 
            || (x == 6 && y == 9) 
            || (x == 10 && y == 7) 
            || (x == 3 && y == 7) 
            || (x == 2 && y == 9) 
            || (x == 4 && y == 8) 
            || (x == 9 && y == 5);
    }

    void whack(int x, int y) {
        if (place(x, y)) {
            if (moleGrid[x - 1][y - 1] == 'W') {
                System.out.println("Already attempted! \'try other co-ordinates\' \n");
            } else {
                moleGrid[x - 1][y - 1] = 'W';
                this.score ++;
                this.molesLeft --;
            }
        }
    }

    void printGridToUser() {
        System.out.println("your score is " + score + " and " + molesLeft + " moles are left. \n");
        System.out.println("input x = -1 and y = -1 to quit the game! \n");
        for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
                System.out.print(" " + moleGrid[i][j] + " ");
            }

            System.out.println("\n");
        }
    }

    void printGrid() {
        for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
                this.moleGrid[i][j] = '*'; 
            }
        }
    }

    public static void main(String[] args) {
        WhackAMole game;

        System.out.println("Lets play the Whack A Mole!\n");

        game = new WhackAMole(50, 100);
        game.printGrid();
        game.printGridToUser();
        Scanner scanner = new Scanner(System.in);

        while ((game.numAttempts > 0) || (game.molesLeft > 0)) {
            System.out.println("Enter box co-ordinate\n");
            System.out.println("x co-ordinate: \n");
            int x = scanner.nextInt();

            System.out.println("y co-ordinate: \n");
            int y = scanner.nextInt();

            if (x == -1 && y == -1) {
                break;
            } else if ((x < 1 || y < 1) || (x > 10 || y > 10)) {
                System.out.println("please enter values of x and y greater than 0 and less than 11! \n");
            } else {
                game.whack(x, y);
                game.numAttempts--;
                game.gridDimension--;
                System.out.println("you can have upto " + game.numAttempts + " out of " + game.gridDimension + " boxes \n");
                game.printGridToUser();
            }
        }

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (game.place(i+1, j+1) && game.moleGrid[i][j] != 'W'){
                    game.moleGrid[i][j] = 'M';
                }
            }
        }

        game.printGridToUser();
        scanner.close();
        System.out.println("game over!!!\n");
        }
    }

2 个答案:

答案 0 :(得分:0)

您的循环正在使用或用于测试功能。这意味着两个条件雾都是假的,以便它停止。在你的情况下。如何写它你必须耗尽数量并且没有留下痣。

更改使用&amp;&amp; vs ||。

答案 1 :(得分:0)

由于您在while循环中使用||,因此while循环未结束。 ||正在使你的循环运行,直到尝试允许,即50和正确的猜测,即找到正确的摩尔都得到满足。所以即使游戏玩家完成了他的允许尝试但没有猜到所有正确的鼹鼠位置,循环不会结束

简单的解决方案是替换||与&amp;&amp;

while ((game.numAttempts > 0) && (game.molesLeft > 0)) 

并且避免在for循环中使用固定数字,即10,而是使用

for (int i = 0; i < game.gridDimension; i++) {
            for (int j = 0; j < game.gridDimension; j++) {

我希望它有所帮助