我一直在这个问题上停留了很长时间,基本上它是一个人类与计算机,我输入说 Rock,它给了我你选择了 Rock 你赢了,即使我选择了其他人它仍然返回那个,有人可以帮助我弄清楚为什么它一直这样做吗?
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
语句。您需要使用and
语句。以你的第一行为例:
if (playerSelection === 'Rock' || computerSelection === 'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
}
它说:
if playerSelection equals Rock OR computerSelection equals Scissors return
因此,如果玩家选择是摇滚,它将返回那里。您需要使用的是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));
倒数第三行的球员选择中还有一个错别字。玩家选择字符串也应该像它正在比较的字符串一样大写。