【问题标题】:JavaFX: Nested for loops for placing rectangles isn't workingJavaFX:用于放置矩形的嵌套 for 循环不起作用
【发布时间】:2019-05-16 16:00:22
【问题描述】:

我正在尝试通过使用 for 循环和创建不同颜色的矩形来创建棋盘。我正在使用两个嵌套的 for 循环:第一个处理以黑色方块开头的所有行,第二个处理以红色方块开头的所有行。我有一个 x 坐标列表和两个单独的 y 坐标列表:y1coords 是所有 y 坐标,对应于以黑色正方形开头的行,y2coords 是所有 y 坐标,对应于以红色正方形开头的行。

//Nested for loop for every row starting with a black square
List<Double> y1coords = Arrays.asList(0.0,150.0,300.0,450.0);
List<Double> xcoords = Arrays.asList(0.0,75.0,150.0,225.0,300.0,375.0,450.0,525.0);
for (int j=0; j<y1coords.size(); j++)
{
  for (int i=0; i<xcoords.size(); i++)
  {
    if (xcoords.get(i)%2 == 0)
    {
      Rectangle square = new Rectangle(75,75,Color.BLACK);
      square.setX(xcoords.get(i));
      square.setY(y1coords.get(j));
      root.getChildren().add(square);
    }
    else
    {
      Rectangle square = new Rectangle(75,75,Color.RED);
      square.setX(xcoords.get(i));
      square.setY(y1coords.get(j));
      root.getChildren().add(square);
    }
  }
}
//Nested for loop for every row starting with a red square
List<Double> y2coords = Arrays.asList(75.0,225.0,375.0,525.0);
for (int j=0; j<y2coords.size(); j++)
{
  for (int i=0; i<xcoords.size(); i++)
  {
    if (xcoords.get(i)%2 != 0)
    {
      Rectangle square = new Rectangle(75,75,Color.BLACK);
      square.setX(xcoords.get(i));
      square.setX(y2coords.get(j));
      root.getChildren().add(square);
    }
    else
    {
      Rectangle square = new Rectangle(75,75,Color.RED);
      square.setX(xcoords.get(i));
      square.setX(y2coords.get(j));
      root.getChildren().add(square);
    }
  }
}

我希望这可以创建一个漂亮的棋盘格,但它总是给我第一行完全黑色,并且不做任何以红色方块开头的行。请访问https://pbs.twimg.com/media/D6szyy7X4AEMGtk.jpg:large 了解我的意思。

【问题讨论】:

  • root的类型是什么?请edit 您的问题包含一个minimal reproducible example,以展示您说明的问题。
  • 在第二次循环中,您设置了两次setX,而不是setX and setY
  • 为什么需要 2 个列表?为什么要检查坐标模 2 而不是索引模 2 的总和?为什么在if/else 的两个块中都做同样的事情,而不是最小化这些块中的代码量,从而最大化两种情况之间共享的代码? Rectangle square = new Rectangle(75, 75, (i+j) % 2 == 0 ? Color.BLACK : Color.RED); square.setX(...); ...
  • 谢谢大家,幸运的是,在我发布这篇文章后不久,我使用一种更简单的方法创建了这个板子,并以这种方式压缩了很多代码。
  • @trashgod 这是一个群组类型,很抱歉造成混乱这是我发布的第一个问题,所以我对此很陌生

标签: java javafx iteration javafx-8 nested-loops


【解决方案1】:

在第二个嵌套 for 循环中编辑您的代码

来自

square.setX(y2coords.get(j));

square.setY(y2coords.get(j));

最终结果是

                if (xcoords.get(i) % 2 != 0) {
                    Rectangle square = new Rectangle(75, 75, Color.BLACK);
                    square.setX(xcoords.get(i));
       /*here*/     square.setY(y2coords.get(j));
                    root.getChildren().add(square);
                } else {
                    Rectangle square = new Rectangle(75, 75, Color.RED);
                    square.setX(xcoords.get(i));
     /*and here*/   square.setY(y2coords.get(j));
                    root.getChildren().add(square);
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多