我正在嘗試用 html、css、js 制作一個簡單的 tiktakto 游戲。我正在制作一個游戲模式,您可以在其中與計算機對抗。這段代碼應該檢查一個列是否應該被機器人阻止:
for(let i = 0; i <= 2; i )
{
for(j = 0; j <= 2; j )
{
console.log("j = ", j)
S = board[j][i];
} // <-- inside for loop j only goes from 0 to 2
if(S === 2) // <-- inside of this if statemnt j is 3
{
console.log("j = ", j)
if(prevent(i, j, "column")) return 1;
else return 0;
}
S = 0;
}
然而,在 for 回圈之后的某個地方, j 的值隨機地從 2 變為 3,然后才能執行 prevent() 函式,為什么?
uj5u.com熱心網友回復:
這個for回圈:
for (j = 0; j <= 2; j ) {
console.log("j = ", j)
S = board[j][i];
}
相當于這個while回圈:
j = 0
while (j <= 2) {
console.log("j = ", j)
S = board[j][i];
j
}
所以 ifj <= 2是truebody 回圈體被執行,也是j .
uj5u.com熱心網友回復:
僅當條件評估為假時,for回圈才會停止。在這里,第一次j <= 2是false當j已經遞增到3。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492936.html
標籤:javascript
下一篇:在React中將物件方法分配為DOM事件處理程式時,我在哪里可以找到關于為什么`this`背景關系未定義的檔案?
