Java脚本游戏的可疑错误

时间:2015-04-26 12:26:17

标签: javascript

所以我一直在对codecademy进行一些练习并遇到一个小问题。目标是用java脚本创建一个石头剪刀游戏。

当我尝试运行我的代码时出现以下错误:"糟糕,再试一次。您的代码已退回'纸张获胜'而不是' undefined'当输入是剪刀和纸张时#34;

我已将问题缩小到"否则如果"我将在下面重点介绍:

    var userChoice = prompt("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 if (choice2 === "paper")
        {
            return ("paper wins");***
        }

    else if (choice1 === "paper") 
    {
        if (choice2 === "rock") 
        {
            return "paper wins";
        }  

    }    
             else if (choice2 === "scissors")
            {
                return "scissors wins";
            }


                else if (choice1 === "scissors")
                {
                    if (choice2 === "rock")
                    {
                        return "rock wins";
                    }
                }

                else if (choice2 === "paper")
                {
                    return "scissors wins";
                }


};

compare(userChoice,computerChoice);

所以有趣的是,如果我创建的代码突显了以下内容:

***
  

否则if(choice2 ===&#34; paper&#34;)           {               if(choice1 === rock)               {

    return ("paper wins");
        }
    }

然后一切正常运转。

我想知道为什么我会收到错误,如果代码看起来非常合理,我首先只使用&#34突出显示的代码;否则如果&#34;声明?

4 个答案:

答案 0 :(得分:0)

我编辑了你的逻辑进行比较,我想你也应该区分谁赢了(即计算机或用户)。

var userChoice = prompt('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);
console.log('User Choice: ' + userChoice);
var compare = function (choice1, choice2)
{
  var _result;
  if (choice1 === choice2)
  {
    _result = 'The result is a tie';
  } 
  else
  {
    if (choice1 === 'rock') {
      if (choice2 === 'scissors') {
        _result = 'rock wins';
      } else if (choice2 === 'paper') {
        _result = 'paper wins';
      }
    } else if (choice1 == 'scissors') {
      if (choice2 === 'rock') {
        _result = 'scissors wins';
      } else if (choice2 === 'paper') {
        _result = 'scissors wins';
      }
    } else if (choice1 == 'paper') {
      if (choice2 === 'rock') {
        _result = 'paper wins';
      } else if (choice2 === 'scissors') {
        _result = 'scissors wins';
      }
    }
  }
  return _result;
}
alert(compare(computerChoice, userChoice));

尝试this小提琴。

感谢。

答案 1 :(得分:0)

您的ifelse if语句没有正确嵌套。

var userChoice = prompt("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 if (choice2 === "paper")
        {
            return ("paper wins");
        }
    }
    else if (choice1 === "paper") 
    {
        if (choice2 === "rock") 
        {
            return "paper wins";
        }
        else if (choice2 === "scissors")
        {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors")
    {
        if (choice2 === "rock")
        {
            return "rock wins";
        }
        else if (choice2 === "paper")
        {
            return "scissors wins";
        }
    }
};

compare(userChoice,computerChoice);

答案 2 :(得分:0)

说实话,你的代码看起来很混乱 每当我的代码变得混乱时,我都会尝试重新思考解决方案。通常&#34;最容易理解&#34;解决方案是最好的&#34;解决方案。

因此,首先要确定规则。在您的情况下,规则是预定义的。

  • 摇滚 节拍 剪刀
  • 剪刀 节拍 纸张
  • 论文 节拍 摇滚

然后,确保在编写代码时遵循规则。确保不要遗漏任何可能的情况 现在,第一个玩家可以有3个可能的动作,如下所示:

if ( choice1 === "rock" ) {

} else if ( choice1 === "paper" ) {

} else if (  choice1 === "scissors" ) {

}

对于这3个选项中的每一个,第二个玩家也可以有3个可能的移动 例如:

if ( choice1 === "rock" ) {

     if ( choice2 === "rock" ) {
        return "it's a tie";
     } else if ( choice2 === "scissors" ) {
        return "rock beats scissors (player1 wins)"
     } else if ( choice2 === "paper" ) {
        return "paper beats rock (player2 wins)"
     }

}
...

然后对其他两种情况执行相同操作,但请确保您的代码在3个主要分支中的每个分支内返回结果。在代码对你有意义并且你测试它之后,你可以做其他的优化,比如检查选择是否相等,这样你就不必在每个分支中做到这一点(对你这样做的方式),但是记得先找出最容易理解的解决方案。

答案 3 :(得分:0)

我有同样的问题,代码看起来很相似,我已经测试过并且工作正常,只有codeacademy的消息似乎没有用,如果我没错。

var userChoice = prompt("Do you choose rock, paper or scissors?");

console.log("User: " + userChoice);

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!";
        console.log("The result is a tie!");
    }
    else if (choice1 === "rock") {

        if (choice2 === "scissors") {
            return "rock wins";
        }
        else if (choice2 === "paper") {
            return "paper wins";
        }
    }

    else if (choice1 === "paper") {

        if (choice2 === "rock") {

            return "paper wins";
        }
        else if (choice2 === "scissors") {
            return "scissors wins";
        }


    }

    else if (choice1 === "scissors") {

        if (choice2 === "rock") {

            return "rock wins";
        }
        else if (choice2 === "paper") {
            return "paper wins";
        }


    }


};        

compare(userChoice, computerChoice)