我不明白为什么我的嵌套循环起作用
目的是根据定义的行数和列在画布上绘制许多正方形:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要慢慢阅读代码。如上所述,两个循环彼此分离。另外,用它们的值替换变量可能很有用:
}
您可以首先调用正方形函数,然后使用i = 0 j = 0,然后
继续进行此操作,直到最大值i = 8和j = 10 。
所有这些平方函数将以这些参数为特定顺序调用。如果您想进一步洞察力,请告诉该正方形功能。
问候。
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:
}
As you can see the square function first is called with i=0 j=0 then it's:
keep doing this exercise until the maximum value of i=8 and j=10.
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.
内部循环将对主循环的每个执行执行
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 executenumberofColumns
*numberofRows
time'sBandolero的答案很棒(+1)!
这是您草图的评论版本,可将每个迭代步骤视为一个动画框架:
本质上,它不是在一个帧中使用 ,而是使用每个新框架来增加。
请随时将
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:
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 theSPACE
key to reset the animation (andc/C
to decrease/increasecolumns
andr/R
to decrease/increase rows)