Javascript 为什么我的石头剪刀游戏不起作用?它不断返回“你选择了摇滚,你赢了”,我该如何解决这个问题?

Javascript 为什么我的石头剪刀游戏不起作用?它不断返回“你选择了摇滚,你赢了”,我该如何解决这个问题?,javascript,Javascript,我已经在这上面呆了很长时间了,基本上这是一个人对电脑,我输入说摇滚,它给我你选择的摇滚你赢了,即使我选择了其他的,它仍然返回,有人能帮我找出为什么它一直这样做吗 let person = prompt ("Rock, Paper, Scissors"); // Computer makes a choice function computerPlay () { let compchoice = ['Rock', 'Paper', 'Scissors']; return compchoic

我已经在这上面呆了很长时间了,基本上这是一个人对电脑,我输入说摇滚,它给我你选择的摇滚你赢了,即使我选择了其他的,它仍然返回,有人能帮我找出为什么它一直这样做吗

 let person = prompt ("Rock, Paper, Scissors");

 // Computer makes a choice
 function computerPlay () {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() * 
compchoice.length)];
}

//Player vs Computer
function playRound (playerSelection, computerSelection) {
 if (playerSelection === 'Rock' || computerSelection === 
'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
 } else if (playerSelection === 'Paper' || computerSelection === 
'Rock') 
{
return 'You chose ' + playerSelection + ',' + ' You win!';
 } else if (playerSelection === 'Scissors' || computerSelection === 
'Paper') 
{
return 'You chose ' + playerSelection + ',' + ' You win!';
 } else if (computerSelection === 'Rock' || playerSelection === 
'Scissors') 
{
return 'Computer chose ' + computerSelection + ',' + 'Computer 
wins!';
} else if (computerSelection === 'Paper' || playerSelection === 
'Rock') 
{
return 'Computer chose ' + computerSelection + ',' + 'Computer 
wins!';
} else if (computerSelection === 'Scissors' || playerSelection === 
'Paper') 
{
return 'Computer chose ' + computerSelection + ',' + 'Computer 
wins!';
} else if (computerSelection === playerSelection) {
return 'Its a draw!';
}else {
return 'Please chose Rock, Paper, or Scissors';
}
}

const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
它应该只是玩一个普通的石头剪刀游戏,如果我选择了石头,电脑选择了纸,电脑应该会赢。现在我试着让它只玩一轮。

您使用的是or语句。您需要使用和语句。以您的第一行为例:

if (playerSelection === 'Rock' || computerSelection === 'Scissors') {
    return 'You chose ' + playerSelection + ',' + ' You win!';
} 
它说:

if playerSelection equals Rock OR computerSelection equals Scissors return 
所以如果playerSelection是摇滚乐,它会马上回来。您需要使用的是AND语句。试试这个:

let playerSelection = prompt("Rock, Paper, Scissors");

// Computer makes a choice
function computerPlay() {
    let compchoice = ['Rock', 'Paper', 'Scissors'];
    return compchoice[Math.floor(Math.random() * compchoice.length)];
}

//Player vs Computer
function playRound(playerSelection, computerSelection) {
    if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
        return 'You chose ' + playerSelection + ',' + ' You win!';
    } else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
        return 'You chose ' + playerSelection + ',' + ' You win!';
    } else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
        return 'You chose ' + playerSelection + ',' + ' You win!';
    } else if (computerSelection === 'Rock' && playerSelection === 'Scissors') {
        return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
    } else if (computerSelection === 'Paper' && playerSelection === 'Rock') {
        return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
    } else if (computerSelection === 'Scissors' && playerSelection === 'Paper') {
        return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
    } else if (computerSelection === playerSelection) {
        return 'Its a draw!';
    } else {
        return 'Please chose Rock, Paper, or Scissors';
    }
}

//const playerSelection = 'Rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

最后一行第三行的玩家选择也有错误。playerSelection字符串应该像它正在比较的字符串一样大写