Java 我在while循环中遇到了麻烦

Java 我在while循环中遇到了麻烦,java,Java,我正在用Java制作一个简单的“打鼹鼠”游戏。为了简单起见,我创建了一个10*10的盒子,并在随机框中放置了10摩尔。我想在用户花费了50次输入或找到了全部10摩尔时退出游戏,但在终止while循环时似乎存在问题,即使用户尝试指定的输入 是实例变量范围问题吗?为什么它不起作用 public class WhackAMole { int score = 0, molesLeft = 10, attempts; char[][] moleGrid = new char[10][10

我正在用Java制作一个简单的“打鼹鼠”游戏。为了简单起见,我创建了一个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");
        }
    }
公共类怪人{
智力得分=0,摩尔分数=10,尝试次数;
char[][]moleGrid=新char[10][10];
int numatempts,gridDimension;
公共怪圈(int numtempts,int gridDimension){
//TODO自动生成的构造函数存根
this.numatempts=numatempts;
this.gridDimension=gridDimension;
}
布尔位置(整数x,整数y){
返回(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);
}
无效重击(整数x,整数y){
如果(放置(x,y)){
if(moleGrid[x-1][y-1]=='W'){
System.out.println(“已尝试!\'尝试其他坐标\'\n”);
}否则{
分子筛[x-1][y-1]=“W”;
这个是.score++;
这是莫莱夫特——;
}
}
}
void printGridToUser(){
System.out.println(“您的分数为“+score+”,剩下“+molesLeft+”摩尔数。\n”);
System.out.println(“输入x=-1和y=-1退出游戏!\n”);
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
System.out.print(“+moleGrid[i][j]+”);
}
System.out.println(“\n”);
}
}
void printGrid(){
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
这个.moleGrid[i][j]='*';
}
}
}
公共静态void main(字符串[]args){
疯狂游戏;
System.out.println(“让我们来玩打鼹鼠!\n”);
游戏=新怪人(50100);
game.printGrid();
game.printGridToUser();
扫描仪=新的扫描仪(System.in);
而((game.numatempts>0)| |(game.molesLeft>0)){
System.out.println(“输入框坐标\n”);
System.out.println(“x坐标:\n”);
int x=scanner.nextInt();
System.out.println(“y坐标:\n”);
int y=scanner.nextInt();
如果(x==-1&&y==-1){
打破
}else如果((x<1 | | y<1)| |(x>10 | | y>10)){
System.out.println(“请输入大于0小于11的x和y值!\n”);
}否则{
游戏。重击(x,y);
game.numAttempts--;
game.gridDimension--;
System.out.println(“您最多可以有“+game.numAttempts+”出“+game.gridDimension+”框\n”);
game.printGridToUser();
}
}
对于(int i=0;i<10;i++){
对于(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(“游戏结束!!!\n”);
}
}

您的循环正在对测试函数使用or。这意味着两个条件都必须为false才能停止。就你而言。怎么写的,你必须用尽所有的珠子,没有痣留下


更改为使用&&vs | |

您的
while
循环没有结束,因为您正在
while
循环中使用
|
|
将使您的循环运行,直到满足允许的尝试(即50次)和正确的猜测(即找到正确的鼹鼠)。
因此,即使玩家完成了允许的尝试,并且没有猜到所有正确的鼹鼠位置,循环也不会结束

简单的解决方案是将| |替换为&

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++) {
for(int i=0;i

我希望这有帮助

您是否尝试过
而((game.numAttempts>0)和(&(game.molesLeft>0))
?并提示:您希望我们花时间来帮助您;因此,请您花2分钟来正确缩进所有输入。