【问题标题】:stuck on the if on Rock, paper, scissors卡在石头剪刀布上
【发布时间】: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


【解决方案1】:

解决方案

您错过了 getUserChoice 方法中的 return 关键字。

function getUserChoice(){
    return prompt("Please enter 'rock', 'paper', 'scissors': ").toLowerCase();
}

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 1970-01-01
    • 2021-01-10
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2016-01-16
    相关资源
    最近更新 更多