我不明白为什么我的嵌套循环起作用

发布于 2025-01-26 08:16:31 字数 630 浏览 2 评论 0原文

目的是根据定义的行数和列在画布上绘制许多正方形:

int squareSize = 20;
int distance = squareSize + 10;
int numberofRows = 8;
int numberofColumns = 10;

for (int rows = 0; rows < numberofRows; rows++)
{

    for(int columns = 0; columns < numberofColumns ; columns++)
     {
        square(30 + (distance * rows), 30 + (distance * columns), squareSize);

     }
}

当行数较低时,我对程序仍然如何执行()循环中的代码感到困惑比列数。我以为我会遇到问题,因为我理解只有在外循环被证明是正确的情况下,只有在内部循环才会被读取和执行(因此,在这种情况下,当程序在数量的数量时会停止工作达到8.)

...但是,当我插入比列数低的行时,该程序仍然给出了预期的结果。这是为什么? 编辑:英语不是我的母语,所以我在问题上犯了一个非常愚蠢的错误。显然,我的问题是,当行数 比列数低时,如何执行代码。我已经相应地编辑了帖子。

The objective is to draw a number of squares on a canvas depending on the defined number of rows and columns:

int squareSize = 20;
int distance = squareSize + 10;
int numberofRows = 8;
int numberofColumns = 10;

for (int rows = 0; rows < numberofRows; rows++)
{

    for(int columns = 0; columns < numberofColumns ; columns++)
     {
        square(30 + (distance * rows), 30 + (distance * columns), squareSize);

     }
}

I'm confused as of to how the program still executes the code in the inner for() loop when the number of rows is lower than the number of columns. I thought I'd have a problem since it was to my understanding that the inner loop would only be read and executed only and only if the outer loop proved to be true (so in this case, the program would stop working when the number of rows reached 8.)

...Yet the program still gives the expected results when I insert a lower number of rows than of columns. Why is that?
EDIT: English isn't my first language so I made a very stupid mistake in my question. Obviously my question is how the code is executed when the number of rows is lower than the number of columns. I've edited the post accordingly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖心男生 2025-02-02 08:16:31

您需要慢慢阅读代码。如上所述,两个循环彼此分离。另外,用它们的值替换变量可能很有用:

for (int i= 0; i< 8; i++)
{

    for(int j = 0; j< 10; j++)
     {
        square(30 + (30* i), 30 + (30* j), 20);

     }

}

您可以首先调用正方形函数,然后使用i = 0 j = 0,然后

square(30,30,20).
Then i=0, j=1
square(30,60,20)
Then i=0 j=2
square(30,90,20).
...
i=0 j=10
square(30,330,20)
Then the inner for reached the maximum and then you come again to your outer loop and add one to i. 
i=1 j=0 
square(60,30,20)
i=1 j=1
square(60,60,20)
...
i=1 j=10
square(60,330,20)
...

继续进行此操作,直到最大值i = 8和j = 10 。

square(240,330,20)

所有这些平方函数将以这些参数为特定顺序调用。如果您想进一步洞察力,请告诉该正方形功能。

问候。

You need to read slowly your code. As said, the two loops are detached one from each other. Also replacing the variables with their values might be useful to understand:

for (int i= 0; i< 8; i++)
{

    for(int j = 0; j< 10; j++)
     {
        square(30 + (30* i), 30 + (30* j), 20);

     }

}

As you can see the square function first is called with i=0 j=0 then it's:

square(30,30,20).
Then i=0, j=1
square(30,60,20)
Then i=0 j=2
square(30,90,20).
...
i=0 j=10
square(30,330,20)
Then the inner for reached the maximum and then you come again to your outer loop and add one to i. 
i=1 j=0 
square(60,30,20)
i=1 j=1
square(60,60,20)
...
i=1 j=10
square(60,330,20)
...

keep doing this exercise until the maximum value of i=8 and j=10.

square(240,330,20)

All these square functions will be called in that specific order with those arguments. If you want further insight please tell what this square function does.

Regards.

一生独一 2025-02-02 08:16:31

内部循环将对主循环的每个执行执行numberOfColumns时间。
因此,主循环将执行numberOfrows时间,因此内部循环将执行numberfcolumns* numbersofrows time的时间

The inner loop will execute for numberofColumns time's for every execute of the main loop.
So the main loop will execute numberofRows time's and therefore the inner loop will execute numberofColumns*numberofRows time's

友欢 2025-02-02 08:16:31

Bandolero的答案很棒(+1)!

这是您草图的评论版本,可将每个迭代步骤视为一个动画框架:

int squareSize = 20;
int distance = squareSize + 10;
int numberOfRows = 8;
int numberOfColumns = 10;

int rows;
int columns;
// do we still need to iterate through rows or columns or are we done ?
boolean isUpdating = true;

void setup(){
  size(290, 350);
  frameRate(3);
  reset();
}

void reset(){
  background(255);
  rows = 0;
  columns = 0;
  isUpdating = true;
}

void draw(){
  
  if(isUpdating){
    // render each square
    fill(255);
    float x = 30 + (distance * rows);
    float y = 30 + (distance * columns);
    square(x, y, squareSize);
    // render rows, columns values
    fill(0);
    text(rows + "," + columns, x, y + squareSize * 0.75);
    
    // increment each row
    rows++;
    // if the end of a row is reached, reset the row count and increment the column count
    if(rows >= numberOfRows){
      rows = 0;
      columns++;
    }
    // if we completed columns we're done
    if(columns >= numberOfColumns){
      isUpdating = false;
    }
    
    println("rows=", rows, "/", numberOfRows, "columns=", columns, "/", numberOfColumns);
  }
}

void keyPressed(){
  if(key == ' '){
    reset(); 
  }
  if(key == 'r'){
    numberOfRows--;
  }
  if(key == 'R'){
    numberOfRows++;
  }
  if(key == 'c'){
    numberOfColumns--;
  }
  if(key == 'C'){
    numberOfColumns++;
  }
}

本质上,它不是在一个帧中使用 ,而是使用每个新框架来增加。

请随时将framerate()更改为更快或更慢的东西,然后使用space键来重置动画(c/c/c降低/增加r/r降低/增加行)

Bandolero's answer is great (+1) !

Here is a commented version of your sketch that visualises each iteration step as an animation frame:

int squareSize = 20;
int distance = squareSize + 10;
int numberOfRows = 8;
int numberOfColumns = 10;

int rows;
int columns;
// do we still need to iterate through rows or columns or are we done ?
boolean isUpdating = true;

void setup(){
  size(290, 350);
  frameRate(3);
  reset();
}

void reset(){
  background(255);
  rows = 0;
  columns = 0;
  isUpdating = true;
}

void draw(){
  
  if(isUpdating){
    // render each square
    fill(255);
    float x = 30 + (distance * rows);
    float y = 30 + (distance * columns);
    square(x, y, squareSize);
    // render rows, columns values
    fill(0);
    text(rows + "," + columns, x, y + squareSize * 0.75);
    
    // increment each row
    rows++;
    // if the end of a row is reached, reset the row count and increment the column count
    if(rows >= numberOfRows){
      rows = 0;
      columns++;
    }
    // if we completed columns we're done
    if(columns >= numberOfColumns){
      isUpdating = false;
    }
    
    println("rows=", rows, "/", numberOfRows, "columns=", columns, "/", numberOfColumns);
  }
}

void keyPressed(){
  if(key == ' '){
    reset(); 
  }
  if(key == 'r'){
    numberOfRows--;
  }
  if(key == 'R'){
    numberOfRows++;
  }
  if(key == 'c'){
    numberOfColumns--;
  }
  if(key == 'C'){
    numberOfColumns++;
  }
}

Essentially, instead of using the for in one frame, it's using each new frame to increment.

Feel free to change the frameRate() to something faster or slower and use the SPACE key to reset the animation (and c/C to decrease/increase columns and r/R to decrease/increase rows)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文