**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。
上个月关门了。
改进这个问题
我在我创建的一个游戏中遇到了一个循环问题。我有一个10×10网格的游戏板。这是一个三部分的挑战。第一部分要求20个随机宝藏,我做得很好。第二部分是随机添加20个巨魔。我使用的循环给了我20个宝藏和30个巨魔。我不擅长打圈。我必须遵循这些代码行,不能使用break(我是java新手,但这是我的profs规则)。
private int width = 10;
private int height = 10;
//Create an array to hold buttons in a 10 by 10 grid
private EmptyButton [] buttons = new EmptyButton [width*height];
Random random = new Random();
int counter = 0;
//Using a while loop when counter is less than number of treasures still hidden
while(counter < game.getNumberStillHidden())
{
//Declare insertion variable and set to random to place buttons randomly
int insertionIndex = random.nextInt(buttons.length);
//Using conditional if button at index is null
if(buttons[insertionIndex] == null)
{
//insert a treasure button
buttons[insertionIndex] = new TreasureButton(game, this);
//Use an acumulator for the counter to move on
counter ++;
}
}
while(counter < game.getNumberOfTriesLeft())
{
Random randomOne = new Random();
//Declare insertion variable and set to random to place buttons randomly
int insertionIndex = randomOne.nextInt(buttons.length);
if(buttons[insertionIndex] == null)
{
//insert a treasure button
buttons[insertionIndex] = new TrollButton(game, this);
//Use an acumulator for the counter to move on
counter ++;
}
}
// Loop to add EmptyButton()'s into remaining null indexes within the button array
for (int index = 0; index < buttons.length; index++)
{
// if the current index is null
if (buttons[index] == null)
{
//add an EmptyButton()
buttons[index] = new EmptyButton(newGame, this);
}
}
// Loop to add all of the contents of the buttonGrid into the gamePanel
for (int index = 0; index < buttons.length; index++)
{
//add buttons to panel
gridPanel.add(buttons[index]);
}
1条答案
按热度按时间0h4hbjxa1#
当你使用
如果你有20件宝物,柜台的价值是20。您需要将计数器重置为0或使用其他计数器。