【发布时间】:2020-01-30 21:53:15
【问题描述】:
我目前正在尝试使用形状创建一个 fizzbuzz,但无法正确显示可被 3 和 5 整除的正方形。我已经搜索了答案,但似乎没有人尝试过。
编写一个程序,在屏幕上绘制 25 个水平方向的黑色圆圈。请使用从 0 开始的 for 循环来完成此操作,每次迭代将 iterand 向前递增 1。
然而,
当iterand能被3整除时,改为画一个紫色圆圈 当 iterand 可以被 5 整除时,画一个绿色方块代替 当迭代数可以被 3 AND 5 整除时,画一个蓝色方块来代替
function setup() {
createCanvas(1500, 1500);
ellipseMode(CENTER);
}
function draw() {
background(200);
var y = 100;
// 25 black squares
for (let x = 0; x < 1250; x += 50) {
fill(0);
ellipse(x, y, 50, 50);
// sets the purple circle
if (x % 3 === 0) {
fill(153, 31, 240);
ellipse(x, y, 50, 50);
}
// sets the green squares should be on top
if (x % 5 === 0) {
fill(0, 255, 0);
square(x + 25, y - 25, 50);
}
// sets the last blue square
// issue is the is supposed to be only one at the 15 mark
if (x % 3 == 0 && x % 5 == 0) {
fill(0, 0, 255);
square(x + 25, y - 25, 50);
}
}
}
【问题讨论】:
标签: javascript for-loop if-statement processing p5.js