如何修复线程中的异常" main" main方法中的java.lang.ArrayIndexOutOfBoundsException

时间:2017-09-28 18:11:43

标签: java

我是Java新手,我正在尝试编写一个简单的Whack-A-Mole游戏案例。但是,问题是我在主方法中调用我的方法之一时会遇到麻烦。

以下是代码:

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

public class WhackAMole {
    //specifying instance variables
    int score;
    int molesLeft;
    int attemptsLeft;
    int Dimension;
    char[][] moleGrid = new char[Dimension][Dimension];

    WhackAMole(int numAttempts, int gridDimension) {
    this.molesLeft = 0;
    this.Dimension = gridDimension;
    this.attemptsLeft = numAttempts;
    for (int i = 0; i < moleGrid.length; i++) {
        for (int j = 0; j < moleGrid[i].length; j++) {
        this.moleGrid[i][j] = '*';
        }
    }
    }

    boolean place(int x, int y) {
    if (moleGrid[x][y] == '*') {
        moleGrid[x][y] = 'M';
        molesLeft = molesLeft + 1;
        return true;

    }
    else {
        return false;
    }
    }

    void whack(int x, int y) {
    if (moleGrid[x][y] == 'M') {
        score = score + 100;
        attemptsLeft = attemptsLeft - 1;
        molesLeft = molesLeft-1;
        moleGrid[x][y] = 'W';
    }
    else {
        attemptsLeft = attemptsLeft - 1;
    }
    }

    void printGridToUser() {
    for (int i = 0; i < Dimension ; i++) {
        for (int j = 0; j < Dimension; j++) {
        if (moleGrid[i][j] == 'W') {
            System.out.print("W" + " ");
        }
        else {
            System.out.print("*" + " ");
        }
        }
    }
    }

    void printGrid() {
    for (int i = 0; i < Dimension ; i++) {
        for (int j = 0; j < Dimension; j++) {

            System.out.print(moleGrid[i][j] + " ");
        }
    }
    }

    public static void main(String[] args) {

    WhackAMole NewGame = new WhackAMole (50, 10);
    Random rand = new Random();

    int i = 0;
    while (i < 10){
    int x = 0 + rand.nextInt((9 - 0) + 1);
    int y = 0 + rand.nextInt((9 - 0) + 1);
    NewGame.place(x, y);
    if (NewGame.place(x, y) == true) {
       i = i + 1;
    }
    else {
       i = i;
    }
    }

    Scanner input = new Scanner(System.in);
    System.out.println("You have a maximum of 50 attempts to get all the moles");
    for (int j = 0; j <= NewGame.attemptsLeft; j++) {
        System.out.println("Enter the first coordinates between 1 and 10");
        int x = input.nextInt() - 1;
        System.out.println("Enter the second coordinates between 1 and 10");
        int y = input.nextInt() - 1;
        if (x == -2) {
        if (y == -2) {
            j = NewGame.attemptsLeft + 1;
            NewGame.printGrid();
            System.out.println("The game is over");
        }
        }
        else {
        NewGame.whack(x,y);
        if (NewGame.molesLeft == 0) {
            j = NewGame.attemptsLeft +1;
        }
        }

    }
    }

}

这就是问题

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at WhackAMole.place(WhackAMole.java:25)
    at WhackAMole.main(WhackAMole.java:79)

为什么会这样?

0 个答案:

没有答案