gpt4 book ai didi

Javascript if/else 不工作。 Odin项目的石头、剪刀、布

转载 作者:行者123 更新时间:2023-12-02 22:55:55 26 4
gpt4 key购买 nike

我一直在尝试解决石头剪刀布项目,但我不知道如何做出 if/else 语句。我这样做了很多次,最后我认为我已经接近解决问题了,但问题是,每次运行程序时我都会得到错误的输出。

例如,我使用“Paper”,计算机使用“Rock”,但控制台显示“It's a tie”,但在我编写的代码中,如果玩家选择纸张而计算机选择岩石,则消息应该是'你赢了。纸胜石”。当我选择“纸张”而计算机选择“剪刀”时,也会发生同样的情况。

我使用了 toLowerCase() 方法,但经过多次更改后,即使删除它并在 if/else 中准确写入单词,这似乎也不是问题。

我需要纠正什么?非常感谢!!!

这是代码:

<!DOCTYPE html>
<html>

<head>
<title>Rock Paper Scissors!</title>
</head>

<body>
<script>
//Computer's selection



function computerPlay() {


let selectRandom = Math.floor(Math.random() * 3);
if (selectRandom === 0) {
return 'Rock';
} else if (selectRandom === 1) {
return 'Paper';
} else {
return 'Scissors';
}


}


console.log('Computer chose: ' + computerPlay());


//Play round between humand and computer

function playRound(playerSelection, computerSelection) {

//Change to lowercase
let player = playerSelection.toLowerCase();
let computer = computerSelection.toLowerCase();


//If player chooses rock
if (player === 'rock') {
if (computer === 'rock') {
return 'It\'s a tie. Try again'
} else if (computer === 'paper') {
return 'You loose. Paper beats Rock'
} else {
return 'You win. Rock beats scissors'
}

}
//If player chooses paper
else if (player === 'paper') {
if (computer === 'paper') {
return 'It\'s a tie. Try again'
}
if (computer === 'scissors') {
return 'You loose. Scissors beats Paper'
} else {
return 'You win. Paper beats Rock'
}
}

//If player chooses scissors
else {
if (computer === 'scissors') {
return 'It\'s a tie. Try again'
} else if (computer === 'rock') {
return 'You loose. Rock beats Scissors'
} else {
return 'You win. Scissors beats Paper'
}
}

}

const playerSelection = 'Paper';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
</script>


</body>

</html>

最佳答案

您一定已经知道,每次调用 computerPlay() 时,计算机都会选择一个随机值。

当您在下面的语句中调用 computerPlay() 时,您会得到一个随机选择。

console.log('Computer chose: ' + computerPlay()); // Random selection.

当您稍后在代码中再次调用该函数(如下所示)时,您会得到另一个随机选择。

 const computerSelection = computerPlay(); // Random selection.
console.log(playRound(playerSelection, computerSelection));

这就是您看到的输出与实际结果不一致的原因。

解决方案:console.log()语句移到上面,在选择计算机后执行,并记录返回值,不要调用computerPlay() 再次。如果再次调用它,您将获得另一个随机选择,而不是传递给 playRound() 函数的随机选择。

const computerSelection = computerPlay();
console.log('Computer chose: ' + computerSelection);
console.log(playRound(playerSelection, computerSelection));

工作示例:

<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors!</title>
</head>
<body>
<script>
//Computer's selection

function computerPlay() {
let selectRandom = Math.floor(Math.random() * 3);
if (selectRandom === 0) {
return 'Rock';
} else if (selectRandom === 1) {
return 'Paper';
} else {
return 'Scissors';
}
}

//Play round between human and computer
function playRound(playerSelection, computerSelection) {

//Change to lowercase
let player = playerSelection.toLowerCase();
let computer = computerSelection.toLowerCase();


//If player chooses rock
if (player === 'rock') {
if (computer === 'rock') {
return 'It\'s a tie. Try again'
} else if (computer === 'paper') {
return 'You loose. Paper beats Rock'
} else {
return 'You win. Rock beats scissors'
}

}
//If player chooses paper
else if (player === 'paper') {
if (computer === 'paper') {
return 'It\'s a tie. Try again'
}
if (computer === 'scissors') {
return 'You loose. Scissors beats Paper'
} else {
return 'You win. Paper beats Rock'
}
}

//If player chooses scissors
else {
if (computer === 'scissors') {
return 'It\'s a tie. Try again'
} else if (computer === 'rock') {
return 'You lose. Rock beats Scissors'
} else {
return 'You win. Scissors beats Paper'
}
}
}

const playerSelection = 'Paper';
const computerSelection = computerPlay();
console.log('Computer chose: ' + computerSelection);
console.log(playRound(playerSelection, computerSelection));
</script>
</body>
</html>

关于Javascript if/else 不工作。 Odin项目的石头、剪刀、布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57982962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com