你的if else语句在我的石头剪刀布游戏中有什么问题?

huangapple go评论60阅读模式
英文:

What is wrong with my if else statements for my rock paper scissor game?

问题

I have translated the code for you. Here it is:

function getComputerChoice() {
    const randomNumber = Math.floor(Math.random()*3);
    switch(randomNumber) {
        case 0:
            return "rock";
        case 1: 
            return "paper";
        case 2:
            return "scissors";
    };
}

function getPlayerChoice() {
    return prompt("rock, paper, or scissors?").toLowerCase();
}

function playRound(playerSelection, computerSelection) {
    computerSelection = getComputerChoice();
    playerSelection = getPlayerChoice();

    if (computerSelection === playerSelection) {
        console.log("Tie");
    } else if (computerSelection === "rock" && playerSelection === "scissors") {
        console.log("You lose! Rock beats Scissors");
    } else if (computerSelection === "paper" && playerSelection === "rock") {
        console.log("You lose! Paper beats Rock");
    } else if (computerSelection === "scissors" && playerSelection === "paper") {
        console.log("You lose! Scissors beats Paper");
    } else if (playerSelection === "rock" && computerSelection === "scissors") {
        console.log("You win! Rock beats Scissors");
    } else if (playerSelection === "paper" && computerSelection === "rock") {
        console.log("You win! Paper beats Rock");
    } else if (playerSelection === "scissors" && computerSelection === "paper") {
        console.log("You win! Scissors beats Paper");
    }
}

I've removed the HTML entities (e.g., ") and provided a cleaner version of your JavaScript code.

英文:

I am attempting to make a Rock Paper Scissors game in JavaScript, however, every time I run it the console logs the last option "You win! Scissors beats Paper" even if my selection was something other than scissors. I believe I made an error with my if else statements but maybe the problem comes from some other part.

function getComputerChoice() {
    const randomNumber = Math.floor(Math.random()*3);
    switch(randomNumber) {
        case 0:
            return "rock";
        case 1: 
            return "paper";
        case 2:
            return "scissors";
    };
    }


function getPlayerChoice() {
    prompt("rock paper or scissors?").toLowerCase();
}




 function playRound (playerSelection,computerSelection) {
    computerSelection = getComputerChoice()
    playerSelection = getPlayerChoice()

    

        

    if (computerSelection === playerSelection) {
        console.log  ("Tie");
    }
   
    else if (computerSelection === "rock" && playerSelection === "scissors") {
        console.log ("You lose! Rock beats Scissors");
    }
    else if (computerSelection === "paper" && playerSelection === "rock") {
        console.log ("You lose! Paper beats Rock");
    }
    else if (computerSelection === "scissors" && playerSelection === "paper") {
        console.log ("You lose! Scissors beats Paper");
    }

    else if (playerSelection === "rock" && computerSelection === "scissors") {
        console.log ("You win! Rock beats Scissors");
    }
    else if (playerSelection === "paper" && computerSelection === "rock") {
        console.log ("You win! Paper beats Rock");
    }
    else  (playerSelection === "scissors" && computerSelection === "paper") ;{
        console.log ("You win! Scissors beats Paper");
    }


} 

答案1

得分: 1

有两个问题导致代码无法正常运行。首先,你的函数 getPlayerChoice() 没有返回任何值。结果,playerSelection === undefined 而不是你预期的值。由于你的 if 语句没有考虑到这一点,它们最终都变为 false。这导致了执行你的 else 语句。

应该是:

function getPlayerChoice() {
    return prompt("rock paper or scissors?").toLowerCase();
}

第二个问题是 else 语句内部不应该有条件。

解决方案:

else {
    console.log("You win! Scissors beats Paper");
}

请注意,在你的第一个函数的 switch 语句后面不需要分号,但似乎它并不导致代码故障。

英文:

There are two issues that makes the code not run as it should. The first is that your function getPlayerChoice() does not return any values. As a result, playerSelection === undefined instead of your intended values. Since your if statements don't account for that, they all end up false. This leads to your else statement being executed.

It should be:

function getPlayerChoice() {
    return prompt("rock paper or scissors?").toLowerCase();
}

The second is that else should not have a condition inside it.

Solution:

else {
        console.log ("You win! Scissors beats Paper");
}

Note that you don't need the semicolon after your switch statement in your first function, but it doesn't seem to cause the code to malfunction.

huangapple
  • 本文由 发表于 2023年5月22日 09:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定