我正在做一个课堂项目,在那里我必须创建一个游戏。我有两个类,BoardButton和TreasureButton,它们创建按钮;TreasureButton类扩展了BoardButton类。
在我正在处理的类中,我试图创建一个面板,其中包含一个2D数组(10x10),其中包含20个随机选择的TreasureButton类实例,而BoardButton类的其余80个实例。当我运行这个程序时,我会得到一个运行时错误:
java.lang.ArrayIndexOutOfBoundsException: -1157793070
at GameBoardPanel.<init>(GameBoardPanel.java:46)
at TreasureGame.<init>(TreasureGame.java:22)
at TreasureGame.main(TreasureGame.java:33)抛出错误的行的代码是:
if (gameBoardButtons[row][col] == null)因为我还没有初始化数组,所以我认为数组中所选索引处的值应该设置为null。任何帮助都将不胜感激。
public class GameBoardPanel extends JPanel
{
private BoardButton[][] gameBoardButtons; // BoardButton 2D array that will be used to populate the board
private final int NUM_TREASURE_BUTTONS = 20; // Number of treasure buttons that will be added to the array
private final int NUM_ROWS = 10; // Number of rows in the array
private final int NUM_COLS = 10; // Number of columns in the array
public GameBoardPanel()
{
int treasureButtonInstances = 0; // Used to count the number of TreasureButton instances
// Create the 'gameBoardButtons' array and make it a 10x10 array
gameBoardButtons = new BoardButton[NUM_ROWS][NUM_COLS];
// Build an object from the Random class that chooses a number between 0-9
Random randomNumber = new Random(10);
// Build a while loop that randomly adds 20 different TreasureButton instances to the 'gameBoardButtons' BoardButton array
while (treasureButtonInstances < NUM_TREASURE_BUTTONS)
{
// Obtain two random numbers that will be used to assign a TreasureButton instance to the 'gameBoardButtons' BoardButton array
int row = randomNumber.nextInt();
int col = randomNumber.nextInt();
// If statement that adds an instance of the TreasureButton class if that particular row/column of the 'gameBoardButtons' BoardButton array is empty
if (gameBoardButtons[row][col] == null)
{
// Create an instance of the TreasureButton class and assign it to the particular row/col of our 'gameBoardButtons' BoardButton array
gameBoardButtons[row][col] = new TreasureButton();
// Increment the 'treasureButtonInstances' variable, as an instance of the TreasureButton class has been created
treasureButtonInstances++;
}// End of the if statement
}// End of the while loop
// Build a nested for loop that will populate the rest of the 'gameBoardButtons' BoardButton array
for (int row = 0; row < NUM_ROWS; row++)
{
for (int col = 0; row < NUM_COLS; row++)
{
// If statement that will assign an instance of the BoardButton class if that particular row/col of our 'gameBoardButtons" BoardButton array is empty
if (gameBoardButtons[row][col] == null)
{
// Create an instance of the BoardButton class and assign it to the particular row/col of our 'gameBoardButtons' BoardButton array
gameBoardButtons[row][col] = new BoardButton();
}// End of the if statement
}//End of the nested for loop
}// End of the for loop
}// End of the GameBoardPanel no-arg constructor发布于 2018-12-02 04:57:35
Random构造函数中的参数是seed,它防止每次生成相同的序列,但与随机整数的范围无关。
在大多数情况下,您不必设置种子,只需使用new Random(),java将自行处理种子。
要将生成的数字限制在0到9,您应该调用randomNumber.nextInt(10)而不是randomNumber.nextInt()。
https://stackoverflow.com/questions/53577488
复制相似问题