0

我正在尝试制作摇滚纸.. 游戏,但似乎代码仅在我打领带时才有效。我可能在下车的过程中搞砸了。另外我想问一下您填写的提示窗口上的数字是字符串还是数字?任何帮助表示赞赏。谢谢!

// rock beats scissors (1 beats 3)
// paper beats rock (2 beats 1)
// scissors beat paper (3 beat 2)


 var player1= prompt("Player ONE, choose (1) for rock, (2) for paper, (3) for scissors");
 var player2 = prompt("Player TWO, choose (1) for rock, (2) for paper, (3) for scissors");

 function game (player1,player2)
 {

    if (player1===player2){
    alert("its a tie");
    }
    else
    { 
      if (player1+player2==="4")
      {
        if(player1==="1"){
        alert("Rock beats Scissors, Player one wins");
        }else {
        alert("Rock beats Scissors, Player Two wins");
        }

      }
      if (player1+player2==="3")
        {
         if (player1==="1"){
            alert("paper beats rock, player One wins");
         }else {
          alert ("paper beats rock, player Two wins");
          }
         }  
      if (player1+player2==="5")
        {
          if (player1==="3"){
            alert("scissors beats paper, Player One wins");
            }else{
            alert("scissors beats papaer, player Two wins");
            }

        }
    }
};
game(player1,player2);
4

4 回答 4

2

您正在连接字符串,而不是添加数字,因此您player1+player2==="3"实际上会产生12or 21。您想先将字符串转换为数字。

将此代码放在 tie check 的 else 块的顶部。

player1 = parseInt(player1);
player2 = parseInt(player2);

作为扩展,您需要清理播放器输入以确保它只包含数字,因为如果传递的不是数字字符串,此方法将失败。

于 2012-11-21T09:01:48.180 回答
1

您正在对 player1..2 变量进行字符串连接而不是积分数学。

请注意,您还需要更改比较,而不仅仅是将字符串解析为整数。

尝试:

 var player1= parseInt(prompt("Player ONE, choose (1) for rock, (2) for paper, (3) for scissors"));
 var player2 = parseInt(prompt("Player TWO, choose (1) for rock, (2) for paper, (3) for scissors"));

 function game (player1,player2)
 {

  if (player1===player2){
    alert("its a tie you mofos");
  }
  else
  { 
    if (player1+player2===4)
    {
      if(player1===1){
        alert("Rock beats Scissors, Player one wins");
      }else {
        alert("Rock beats Scissors, Player Two wins");
      }
    }
    if (player1+player2===3)
    {
      if (player1===1){
        alert("paper beats rock, player One wins");
      }else {
        alert("paper beats rock, player Two wins");
      }
    }  
    if (player1+player2===5)
    {
      if (player1===3){
        alert("scissors beats paper, Player One wins");
      }else{
        alert("scissors beats papaer, player Two wins");
      }
    }
  }
};
game(player1,player2);
于 2012-11-21T09:03:17.000 回答
0

除了鲁尔泽写的,还有一个错误:

player1+player2==="4"您尝试添加两个整数然后将它们与字符串进行比较的行中。

===运算符不仅比较值,还比较类型。当您将整数与字符串进行比较时,它将返回 false。

使用比较==器,其中数字 4 等于字符串“4”,或者与数字 4 进行比较:

player1+player2 === 4
于 2012-11-21T09:03:22.260 回答
0

试试这个!希望能帮助到你!

--javascript code for rock, paper, scissors...--

var userChoice = prompt("Please type in your choice : rock , paper or scissors?");
var computerChoice = Math.random();

if (computerChoice < 0.34)
{
    computerChoice = "rock";
}
else if(computerChoice <= 0.67)
{
    computerChoice = "paper";
}
else {
    computerChoice = "scissors";
}

var compare = function (userChoice, computerChioce)
{
    if (userChoice === computerChioce)
    {
        return "The result is a tie!";
    }
    else if (userChoice === "rock")
    {
        if(computerChioce === "scissors")
        {
            return "rock wins" + " ," + "rock breaks scissors";
        }
        else if (computerChioce === "paper")
        {
            return "paper wins" +  " ,"  + "paper captures rock";
        }
    }
    else if (userChoice === "paper")
    {
        if (computerChioce === "rock")
        {
            return "paper wins" +  ", "  + "paper captures rock";
        }
        else if (computerChioce === "scissors")
        {
            return "scissors win" + ", " + "scissors cuts paper";
        }
    }
    else if (userChoice === "scissors")
    {
        if (computerChioce === "rock")
        {
            return "rock wins" + " " + "rock breaks scissors";
        }
        else if (computerChioce === "paper")
        {
            return "scissors win" + ", " + "scissors cuts paper";
        }
    }
};

console.log("You chose" + " " + userChoice + ".");
console.log("Computer chose" + " " + computerChoice + ".");
compare (userChoice, computerChoice);
于 2013-08-22T09:32:26.827 回答