我是 java 新手,我正在尝试编写一个生成 10 个随机框然后删除一个框并添加另一个框的代码。因此,总数仍然是 10 个盒子,但循环继续进行。我已经想出了如何创建 10 个随机框,但我不确定如何从中删除一个。这是代码:
final int width = 800;
final int height = 600;
final int boxWidth = 50;
final int maxBoxes = 10;
this.setSize(width, height);
Random random = new Random();
for(int box=0;box<maxBoxes;box++)
{
int x = random.nextInt(width-boxWidth);
int y = random.nextInt(height-boxWidth);
GRect r = new GRect(x, y, boxWidth, boxWidth);
Color c = new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256));
r.setFilled(true);
r.setFillColor(c);
this.add(r);
this.pause(100);