Javascript 石头、剪刀、布。随机函数未按预期工作
这是我第一次在这个网站上发帖 目前,我正试图在Javascript上构建石头、剪刀、纸牌游戏。我已经写出了代码,但是我在运行代码时遇到了问题 每当我刷新浏览器以玩游戏时,底部的console.log将运行游戏,但computerPlay()将始终处于禁用状态。我试过自己运行computerPlay(),运行正常 任何帮助都将不胜感激Javascript 石头、剪刀、布。随机函数未按预期工作,javascript,Javascript,这是我第一次在这个网站上发帖 目前,我正试图在Javascript上构建石头、剪刀、纸牌游戏。我已经写出了代码,但是我在运行代码时遇到了问题 每当我刷新浏览器以玩游戏时,底部的console.log将运行游戏,但computerPlay()将始终处于禁用状态。我试过自己运行computerPlay(),运行正常 任何帮助都将不胜感激 let userPlay; do { userPlay = prompt("rock, paper, or
let userPlay;
do {
userPlay = prompt("rock, paper, or scissors?");
}
while (userPlay !== "rock" && userPlay !== "paper" && userPlay !== "scissors"); //prompt user for rock, scissors, or paper.
let compPlay = computerPlay();
function computerPlay() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "Draw!";
} else if (computerSelection === "paper") {
return "You lose! Paper beats rock.";
} else {
return "You win! Rock beats scissors.";
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return "You win! Paper beats rock.";
} else if (computerSelection === "paper") {
return "Draw!";
} else {
return "You lose! Scissors beats paper.";
}
} else {
if (computerSelection === "rock") {
return "You lose! Rock beats scissors.";
} else if (computerSelection === "paper") {
return "You win! Scissors beats paper.";
} else {
return "Draw!";
}
}
}
console.log(playRound(userPlay, computerPlay));
您在一个位置为同一事物使用不同的变量名
computerSelection
,在另一个位置为compPlay
。最后,在程序末尾的console.log()
中,您将打印出函数computerPlay
试试这个:
let userPlay = 'scissors';
do {
userPlay = prompt("rock, paper, or scissors?");
}
while (userPlay !== "rock" && userPlay !== "paper" && userPlay !== "scissors"); //prompt user for rock, scissors, or paper.
let computerSelection = computerPlay();
function computerPlay() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "Draw!";
} else if (computerSelection === "paper") {
return "You lose! Paper beats rock.";
} else {
return "You win! Rock beats scissors.";
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return "You win! Paper beats rock.";
} else if (computerSelection === "paper") {
return "Draw!";
} else {
return "You lose! Scissors beats paper.";
}
} else {
if (computerSelection === "rock") {
return "You lose! Rock beats scissors.";
} else if (computerSelection === "paper") {
return "You win! Scissors beats paper.";
} else {
return "Draw!";
}
}
}
console.log(playRound(userPlay, computerSelection));
computerPlay
是一个函数,不是字符串,您忘记调用它了。考虑使用精确的变量名来避免这些错误,我不理解下注。提问者是一个新用户,我帮他发现了错误。不是我,但这类问题是离题的,将来对任何人都没有用处——与其回答,不如关闭并删除。这不仅对网站更有利,我也看到这样的答案通常也会吸引反对票