[英]Conditional statement not functioning as expected: Javascript
我正在尝试在用户和计算机之间制作一个小的石头、剪纸和剪刀程序。 除了 gameRound() 条件语句外,一切似乎都运行良好。 用户输入是什么并不重要。 它只是运行 (else) 语句。
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
// the logic that decides who wins and loses
const gameRound = (playerSelection, computerSelection) => {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
// the function that begins the game
const game = () => {
for(let i = 0; i < 5; i++) {
return gameRound();
}
}
console.log(game());
由于新声明的参数playerSelection
和computerSelection
, gameRound
条件语句无法按预期运行。 所以它必须是一个函数语句而不是内联箭头函数。
然后,Javascript代码如下:
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
for (let i = 0; i < 5; i++) {
console.log(gameRound(playerSelection, computerSelection));
}
或者如果你的意思是玩 5 轮剪刀石头布,那么playerSelection
和computerSelection
变量的分配应该在for
循环内,如下所示:
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
for (let i = 0; i < 5; i++) {
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
console.log(gameRound(playerSelection, computerSelection));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.