It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




7年前关闭。





我是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);

最佳答案

您需要将框值存储在数组中。然后,在循环的每次迭代中,擦除屏幕并重新绘制所有10个框。然后将一个框的框值随机替换并重复。

10-04 18:23