java - 如何让一个形状在for循环中只出现一次?

标签 java javafx javafx-8

我试图让星形只出现一次。我尝试使用 while 函数,但似乎无法使其适用于形状,但设法适用于扫描仪。我是否需要在星号条件代码中或 for 循环开始之前插入 while 循环代码?这是我的代码:

 public void start(Stage primaryStage) {
    GridPane grid = new GridPane();
    grid.setVgap(5);
    grid.setHgap(5);
    Random rand = new Random();

    //Shapes
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                int shapePick = rand.nextInt(5);
                if (shapePick == 0) {//CIRCLE
                    Circle circle = new Circle(0, 0, 25);
                    circle.setFill(Color.RED);
                    circle.setStroke(Color.BLACK);
                    grid.add(circle, i, j);
                } else if (shapePick == 1) {//SQUARE
                    Rectangle rect = new Rectangle(0, 0, 50, 50);
                    rect.setFill(Color.BLUE);
                    rect.setStroke(Color.BLACK);
                    grid.add(rect, i, j);
                } else if (shapePick == 2) {//TRIANGLE
                    Path tri = new Path();
                    MoveTo moveTo = new MoveTo(25, 0);
                    LineTo line1 = new LineTo(0, 50);
                    LineTo line2 = new LineTo(50, 50);
                    LineTo line3 = new LineTo(25, 0);

                    tri.getElements().add(moveTo);
                    tri.getElements().addAll(line1, line2, line3);
                    tri.setFill(Color.GREEN);
                    tri.setStroke(Color.BLACK);
                    grid.add(tri, i, j);
                } else if (shapePick == 3) {//HEXAGON
                    Polygon hexa = new Polygon(25.0, 0.0, 0.0, 20.0, 0.0, 40.0, 25.0, 60.0, 50.0, 40.0, 50.0, 20.0);
                    hexa.setFill(Color.PURPLE);
                    hexa.setStroke(Color.BLACK);
                    grid.add(hexa, i, j);
                } else {
                        Polygon star = new Polygon(25, 0, 15, 20, 0, 20, 10, 40, 5, 60, 25, 50, 45, 60, 40, 40, 50, 20, 35, 20);
                        star.setFill(Color.YELLOW);
                        star.setStroke(Color.BLACK);
                        grid.add(star, i, j);
                    }
                }

            }

Here is the output

最佳答案

由于星星的数量最多,因此限制了选择范围。

int possible=5;
//Shapes
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        int shapePick = rand.nextInt(possible);
        //....
        else {
                    Polygon star = new Polygon(25, 0, 15, 20, 0, 20, 10, 40, 5, 60, 25, 50, 45, 60, 40, 40, 50, 20, 35, 20);
                    star.setFill(Color.YELLOW);
                    star.setStroke(Color.BLACK);
                    grid.add(star, i, j);
                    possible = 4;
                }

关于java - 如何让一个形状在for循环中只出现一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61007725/

相关文章:

java - 将对象从服务发送到应用程序 - 处理程序

gradle - 如何改进/更改原始的Gluon Charm Down服务,并在我的应用程序中使用它?

javafx - TableColumn 在场景中的位置

javafx-2 - 如何在 JavaFX 中创建导航菜单

java - 将运行时参数传递给包含注入(inject)的对象

java - 将 Angular JS 中的数据作为表单数据发布到 Java REST Web 服务时出现问题

css - 在 JavaFx TreeTableView 上应用 CSS 失败

java - 如何从扩展 Activity 重写方法?

java - 超时后自动关闭对话框并取消响应