どのような形状でのみループのために一度現れる作るには?

Solomid1280:

私は一度だけ表示される星形を作るしようとしています。私はしばらくの機能を使用してみました、それは形のために動作させるように見えるカントが、スキャナ用にするために管理します。私は私のスター条件コードまたは先頭の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);
                    }
                }

            }

ここで出力されます

マット:

スター最大数があるので、選択の幅を制限します。

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;
                }

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=396977&siteId=1