我是一个Java初学者,我正在尝试编写一个程序,其中白球不断出现在屏幕上。一个球出现在下一个球之前应该有几秒钟的停顿,并且球需要出现在屏幕上的不同位置。我需要帮助使用 RandomGenerator 使球出现在不同的地方。任何帮助将不胜感激!
私有(private)RandomGenerator rgen = new RandomGenerator();
//~ Constructor ...........................................................
// ----------------------------------------------------------
/**
* Creates a new BubbleGame object.
*/
public void init()
{
//call method to create regions
CreateRegions();
//add mouse listeners
addMouseListeners();
//loop to add bubbles
while (true)
{
//create a filled bubble
GOval bubble = new GOval (100, 100, 50, 50);
bubble.setFilled(true);
bubble.setColor(Color.WHITE);
//randomly generate coordinates within the field
int rgen =
//add the bubble and pause
add(bubble);
Thread.sleep(3000);
}
}
最佳答案
您可以使用Random.nextInt(maxInt)
生成0到maxInt-1之间的数字。
Random rnd = new Random();
rnd.nextInt(10); // 0-9
关于java - 让球不断出现在不同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19995915/