嵌套循环问题
Issue with a Nested Loop
总的来说,我是 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
。外环测试永远不会失败,所以它会继续。
使用两个单独的计数器变量。此外,缩进代码以反映其结构。
总的来说,我是 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
。外环测试永远不会失败,所以它会继续。
使用两个单独的计数器变量。此外,缩进代码以反映其结构。