gpt4 book ai didi

javascript - 嵌套循环的问题

转载 作者:行者123 更新时间:2023-11-28 18:42:42 25 4
gpt4 key购买 nike

总的来说,我对 javascript 和编码很陌生。

我试图在 Canvas 上以随机坐标绘制一系列圆圈,并尝试根据其他一些值确定这组圆圈的 fillStyle ,为简单起见,此处未包含这些值。谁能告诉我为什么我会在这里遇到无限循环?

for (var counter = 1; counter <= 30; counter++) {
var xCenter = 154;
var yCenter = 270;
var accuracyMod = (300 + 800) * 0.0013;

var xMax = (xCenter + (100 - 50) * accuracyMod);
var xMin = (xCenter - (100 - 50) * accuracyMod);
var yMax = ((yCenter + (100 - 50) * accuracyMod)) - (100 - 50);
var yMin = ((yCenter - (100 - 50) * accuracyMod)) - (100 - 50);

function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

console.log(randomIntFromInterval(xMin, xMax), (randomIntFromInterval(yMin, yMax)));

var stroke = "color";
var fill = "color";
var intense = 85;
for (var counter = 1; counter <= 7; counter++) {
if (intense < 5) {
stroke = "#fffdfc";
fill = "#fffdfc";
} else if (intense <= 20) {
stroke = "#ffebd7";
fill = "#ffebd7";
} else if (intense <= 40) {
stroke = "#ffae5f";
fill = "#ffae5f";
} else if (intense <= 60) {
stroke = "#ff7402";
fill = "#ff7402";
} else if (intense <= 80) {
stroke = "#e25610";
fill = "#e25610";
} else if (intense <= 95) {
stroke = "#b84452";
fill = "#b84452";
} else {
stroke = "#ac3f65";
fill = "#ac3f65";
}

}
}

最佳答案

是的,是的,因为您在外部和内部循环中都使用 counter 作为迭代变量。内部循环每次都会在 8 处离开 counter。外循环测试永远不会失败,所以会继续。

使用两个单独的计数器变量。另外,缩进代码以反射(reflect)其结构。

关于javascript - 嵌套循环的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com