For() 循环在第一个循环中不执行任何操作

For() loop not doing anything in the first loop

我正在做 TOP 的第三个项目(The Odin Project),它包括创建一个简单的剪刀石头布游戏,目前没有 GUI,只是在控制台上。这是我要制作的循环的伪代码:

提示用户输入回合数,分配给变量回合
从 0 开始计数器 (i),对于 i 的值(不包括)轮数,执行以下代码:

我对这段代码没有任何问题,它曾经使用 while 循环而不是 for 循环完美地工作,所以我想我一定是做错了什么。

这是我写的代码:

let rounds = prompt("How many rounds do you want to play?")

for (let i = 0; i < rounds; i++) {
    let playerMove = convertToNumber(promptPlayer())
    let computerMove = computerPlay()
    if (computerMove > playerMove) {
        if (computerMove === 2 && playerMove === 0) {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        } else {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        }
    } else if (playerMove > computerMove) {
        if (computerMove === 0 && playerMove === 2) {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        } else {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        }
    } else if (playerMove) {
    console.log("Ties!")
    }
}

这是对页面行为的详细解释:如果我输入 5,它会提示我 5 次石头、布或剪刀,除了第一个(这就是问题所在)之外,每个都是登录控制台。我不知道为什么第一个被省略了。

我不确定我是否提供了足够的信息,如果没有,我会发送整个代码(6 行,不多)。其余代码只是函数声明,当我将 while 循环更改为 for 循环时,我没有更改它们。

谢谢。

编辑:

看到很多评论都在强调代码不足,所以这里是我写的完整代码:

function computerPlay() {
    return Math.floor(Math.random() * 3)
}

function convertToItem(num) {
    switch (num) {
        case 0:
            return "rock";
        case 1:
            return "paper";
        case 2:
            return "scissors";
    }
}

function convertToNumber(item) {
    switch (item) {
        case "rock":
            return 0;
        case "paper":
            return 1;
        case "scissors":
            return 2;
    }
}

function promptPlayer() {
    let playerMove = prompt("Rock, Paper or Scissors?").toLowerCase()
    if (playerMove !== "rock" && playerMove !== "paper" && playerMove !== "scissors") {
        alert("Invalid answer! Try again.")
    } else {
        return playerMove
    } 
}

let rounds = prompt("How many rounds do you want to play?")

for (let i = 0; i < rounds; i++) {
    let playerMove = convertToNumber(promptPlayer())
    let computerMove = computerPlay()
    if (computerMove > playerMove) {
        if (computerMove === 2 && playerMove === 0) {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        } else {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        }
    } else if (playerMove > computerMove) {
        if (computerMove === 0 && playerMove === 2) {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        } else {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        }
    } else if (playerMove) {
    console.log("Ties!")
    }
}

你的循环确实有效。这就是您收到 5 个提示的原因。但是你的 if-statements 并不总是正确的,所以有时它不会在控制台中输出一些东西。尤其是最后一张,很奇怪。如果其他一切都需要 Ties! 那么只需使用 else 而不是另一个 else if (这可能是错误的并且不会显示消息)

你的示例代码不足以作为一个工作示例,所以我在下面做了一些更改,但循环是相同的,逻辑与 if-statements(所有人都认为我确实稍微改变了顺序所以我不需要嵌套的if)。

let rounds = prompt("How many rounds do you want to play?");

for (let i = 0; i < rounds; i++) {
    let playerMove = Number( prompt("What your gonna do? Rock=0; Paper=1; Scissors=2;") ); // Expect a number 0, 1 or 2
    let computerMove = Math.floor(Math.random() * 3) // random number 0, 1 or 2
    
    // if numbers from playerMove and computerMove are the same it's a tie
    if (playerMove === computerMove) {
        console.log("Ties!");
    } 
    // if player has Rock(0) and computer Paper(1) or player has Paper(1) and computer Scissors(2) you lose..
    // if player has Scissors(2) but computer has Rock(0) you lose as well
    else if (playerMove+1 === computerMove || playerMove-2 === computerMove) {
        console.log("You lost!");
        }
    // in all other cases you win
    else {
            console.log("You win!");
    }
    
    console.log("Played by player: "  + playerMove);
    console.log("Played by computer: " + computerMove);
}