我是编程新手!我正在尝试使用 Processing(一种使用与 Java 类似的语法的语言)制作棋盘。
为什么这不起作用?
void setup(){
//5.b In setup() set the size to 350, 350
size(350,350);
for (int y = 0; y < 8; y = y++){
for (int x = 0; x < 8; x++){
if ((y+x+1) % 2 == 0 ){
fill(255);
}else{
fill(0);
}
rect(50*x,50*y,50,50);
}
}
}
```
Run Code Online (Sandbox Code Playgroud)
程序无法运行,因为这个永远运行的 for 循环:
for (int y = 0; y < 8; y = y++){
Run Code Online (Sandbox Code Playgroud)
原因是该分配y = y++执行以下操作:
yyy步骤 1 中计算的值,即y因此,ynever的值不会改变,for 循环也不会终止。
for (int y = 0; y < 8; y++){
Run Code Online (Sandbox Code Playgroud)