【发布时间】:2022-11-27 04:35:17
【问题描述】:
我是编码新手,我必须做这个剪刀石头布,但找不到我的错误,它仍然每次都显示其他结果。 欢迎任何帮助
// Get the computer choice
function getComputerChoice() {
"use strict";
const computerPlay = Math.floor(Math.random()*3);
if (computerPlay === 1){
return("rock")
}
else if(computerPlay === 2){
return ("paper")
}
else {
return ("scissors")
}
}
// Get the user choice
function getUserChoice(){
prompt("Please enter 'rock', 'paper', 'scissors': ").toLowerCase();
}
// Play a round
function playRound(){
let computerSelection = getComputerChoice();
let playerSelection = getUserChoice();
if (computerSelection === playerSelection){
console.log("Game is a Tie")
}
else if (computerSelection == 'paper' && playerSelection == 'rock'){
console.log("Computer Win")
}
else if (computerSelection == 'rock' && playerSelection == 'scissors'){
console.log("Computer Win")
}
else if (computerSelection == 'scissors' && playerSelection == 'paper'){
console.log("Computer Win")
}
else {
console.log("User Win")
}
}
// Play Game function
function playGame(){
for (let i = 0; i < 5; i++){
playRound()
}
}
playGame()
我尝试如果它似乎错过了正确运行的东西。
【问题讨论】:
-
尝试为这个位添加一个返回语句,这样用户提交的值就会返回到你的比较逻辑中
return prompt("Please enter 'rock', 'paper', 'scissors': ").toLowerCase();
标签: javascript