我无法分配随机数的函数,当我输入我将其设置为



我创建了以下代码,以便计算机播放3种不同的随机结果

function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else(computerRandomNumber === 2)
return "scissors";
}

到目前为止一切都很好!,然后我想运行另一个功能,那里将有玩家VS计算机

function playRound() {

if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} etc. etc.

我已经为播放器和计算机声明了这两个变量,这样上面的函数就可以工作了。

let playerSelection = ["rock", "paper", "scissors"];
let computerSelection = computerPlay();

当我控制台.log(computerSelection(时,我总是得到相同的结果,没有随机值,但当我控制台log computerPlay((时,它运行得很好,我得到的结果是随机的。为什么会发生在我身上?:(

以下是我迄今为止写的全部代码

function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else(computerRandomNumber === 2)
return "scissors";
}
let playerSelection = ["rock", "paper", "scissors"];
let computerSelection = computerPlay();

function playRound() {

if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} else if (playerSelection === "rock" && computerSelection === "scissors") {
return "Bravoooo you made it! Rock smashed the scissors! "
} else if (playerSelection === "paper" && computerSelection === "rock") {
return "Bravoooo you made it! Paper wrapped the rock and threw it in the bin! "
} else if (playerSelection === "paper" && computerSelection === "paper") {
return "It's a tie! Ties are lame and so are you for tying. "
} else if (playerSelection === "paper" && computerSelection === "scissors") {
return "Loooooser looooser nia nia nia! Scissors cut through your paper and made it look like an ugly origami swan"
} else if (playerSelection === "scissors" && computerSelection === "rock") {
return "Loooooser looooser nia nia nia! Rock smashed the scissors. "
} else if (playerSelection === "scissors" && computerSelection === "paper") {
return "Bravoooo you made it! Scissors cut through paper and made it look like an ugly origami swan"
} else(playerSelection === "scissors" && computerSelection === "scissors")
return "It's a tie! Ties are lame and so are you for tying. "
}

您只分配一次变量,永远不会更改它。

如果你想让计算机对每一轮都做出决定,你可以写这样的东西:

function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else {
return "scissors";
}
}
let playerSelectionVariants = ["rock", "paper", "scissors"];
function playRound() {

let computerSelection = computerPlay();
let playerSelection = /* request user input from playerSelectionVariants */
if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} else if (playerSelection === "rock" && computerSelection === "scissors") {
return "Bravoooo you made it! Rock smashed the scissors! "
} else if (playerSelection === "paper" && computerSelection === "rock") {
return "Bravoooo you made it! Paper wrapped the rock and threw it in the bin! "
} else if (playerSelection === "paper" && computerSelection === "paper") {
return "It's a tie! Ties are lame and so are you for tying. "
} else if (playerSelection === "paper" && computerSelection === "scissors") {
return "Loooooser looooser nia nia nia! Scissors cut through your paper and made it look like an ugly origami swan"
} else if (playerSelection === "scissors" && computerSelection === "rock") {
return "Loooooser looooser nia nia nia! Rock smashed the scissors. "
} else if (playerSelection === "scissors" && computerSelection === "paper") {
return "Bravoooo you made it! Scissors cut through paper and made it look like an ugly origami swan"
} else {
return "It's a tie! Ties are lame and so are you for tying. "
}
}

最新更新