繁体   English   中英

Codecademy上的石头,剪刀,剪刀布游戏

[英]Rock,Paper,Scissors Game on Codecademy

我试图结合网站(Codecademy)上给出的其他建议。 有领带的功能和提供再次输入的功能。 这是包含错误的代码:

var userChoice, computerChoice;

var choicesDetermination = function(){
    userChoice = prompt("Do you choose rock, paper or scissors?");
    if(userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors"){
        userChoice = prompt("Invalid input. Please try again.\n Do you choose rock, paper or scissors?");
    }
    computerChoice = Math.random();
    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    } 
    console.log("User: " + userChoice);
    console.log("Computer: " + computerChoice);
}

var compare = function(choice1, choice2){
    if(choice1 === choice2){
        console.log("The result is a tie! Let's try one more time.");
        choicesDetermination();
        compare(userChoice, computerChoice);
    }
    else if(choice1 === "rock"){
        if(choice2 === "scissors"){
            return "rock wins";
        }
        else{
            return "paper wins";
        }
    }
    else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return "paper wins";
        }
        else{
            return "scissors wins";
        }
    }
    else{
        if(choice1 === "scissors"){
            if(choice2 === "paper"){
                return "scissors wins";
            }
            else{
                return "rocks wins";
            }
        }
    }
}
choicesDetermination();
compare(userChoice, computerChoice);

在这里,choicesDetermination()是接收输入并将其存储在userChoice和computerChoice(两个全局变量)中的函数。 我不知道为什么,但是当我再次要求输入时,代码似乎运行良好; 变量已正确更改。 但是功能compare()不能正确运行。 return语句不会显示在屏幕上。

我在Code Academy中经常发现的是,输出字符串必须非常烦人,因此请首先检查!

我认为您的代码存在问题,实际上是您应该return "The result is a tie! Let's try one more time." 不是 console.log

请在下面找到我完整的传递代码:

var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice != "rock" && "scissors" && "paper") {
    alert("Please enter 'rock', 'scissors', or 'paper' as shown.");
    userChoice = prompt("Type carefully, please: Do you choose rock, paper or scissors?");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins"; 
        }
        else {
            return "scissors wins";
        }
    }
    else {
        if (choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors wins";
        }
    }
};

compare(userChoice, computerChoice);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM