JavaScript中使用数组的岩石,纸张,剪刀

问题描述:

使用javascript编写Rock,Paper,Scissors游戏的代码时出现问题。我知道它在这里有一个共同的话题,但我刚刚开始,并没有找到任何具体的使用数组的方式,我试图使用它。我使用了一个数组来使计算机随机生成一个数字,然后我希望它提示用户输入一个数字。这里是我开始:JavaScript中使用数组的岩石,纸张,剪刀

<script type="text/javascript"> 
    var theRanNum = Math.floor(Math.random()*3)+1; 
    deptArray = new Array(3); 
deptArray[0] = "No Pick"; 
deptArray[1] = "Rock"; 
deptArray[2] = "Paper"; 
deptArray[3] = "Scissors"; 
    document.write("Your opponent chose" + deptArray[deptNo]); 
    document.write("<br>"); 
    var myGuess = parseInt(window.prompt("Enter your guess 1-Rock 2-Paper 3-Scissors")); 
    if (theRanNum == myGuess) 
     { 
     document.write("Tie"); 
    } 
    else 
     { 
     if (theRanNum = 1) and (myGuess = 2) 

我不是很确定我是否应该去使IF语句,我开始,或者即使到目前为止我的代码是正确的方式。白屏已经成为我迄今为止最好的朋友。提前致谢。

+0

最后一行必须是'如果(theRanNum == 1 && myGuess == 2){...}',然后你需要关闭你以前的大括号。 –

+0

这是你的全部代码吗?因为这会在当前状态下引发错误。 – AtheistP3ace

+0

'deptArray [deptNo]'应该是'deptArray [theRanNum]'。而且我不确定如果你告诉用户电脑在他们轮到他们之前已经选择了什么,游戏会变得多么有趣... –

<script type="text/javascript"> 
 
    var theRanNum = Math.floor(Math.random()*3)+1; 
 
    deptArray = []; 
 
    deptArray.push("Rock"); 
 
    deptArray.push("Paper"); 
 
    deptArray.push("Scissors"); 
 
    var myGuess = parseInt(window.prompt("Enter your guess 1-Rock 2-Paper 3-Scissors:")); 
 
    if (myGuess > 0 && myGuess < 4) { 
 
     document.write("You chose: " + deptArray[myGuess]); 
 
     document.write("<br>"); 
 
     document.write("PC chose: " + deptArray[1]); 
 
     if (theRanNum == myGuess) { 
 
      document.write("Tie"); 
 
     } else { 
 
      if (theRanNum == 1 && myGuess == 2) 
 
      document.write("You win!"); 
 
      else if (theRanNum == 1 && myGuess == 3) 
 
      document.write("You lose!"); 
 
      else if (theRanNum == 2 && myGuess == 1) 
 
      document.write("You lose!"); 
 
      else if (theRanNum == 2 && myGuess == 3) 
 
      document.write("You win!"); 
 
      else if (theRanNum == 3 && myGuess == 1) 
 
      document.write("You win!"); 
 
      else if (theRanNum == 3 && myGuess == 2) 
 
      document.write("You lose!"); 
 
     } 
 
    } else { 
 
     document.write("<br>"); 
 
     document.write("You chose a wrong number!"); 
 
    } 
 
</script>

+0

我真的不知道“deptArray.push”是做什么的,用户输入的数字对我来说是关闭的,但它清除了一些东西,所以生病了一些更多的测试,谢谢。 – Tstanley12

好吧,Prelite的代码几乎工作,我只是不得不改变document.write("PC chose: " + deptArray[1]);document.write("PC chose: " + deptArray[theRanNum]);和我交换了数组:

<script type="text/javascript"> 
    var theRanNum = Math.floor(Math.random()*3)+1; 
    deptArray = new Array(3); 
deptArray[0] = "No Pick"; 
deptArray[1] = "Rock"; 
deptArray[2] = "Paper"; 
deptArray[3] = "Scissors"; 
    var myGuess = parseInt(window.prompt("Enter your guess 1-Rock 2-Paper 3-Scissors:")); 
    if (myGuess > 0 && myGuess < 4) { 
     document.write("You chose " + deptArray[myGuess]); 
     document.write("<br>"); 
     document.write("PC chose " + deptArray[theRanNum]); 
     if (theRanNum == myGuess) { 
      document.write(": Tie"); 
     } else { 
      if (theRanNum == 1 && myGuess == 2) 
      document.write(": You win!"); 
      else if (theRanNum == 1 && myGuess == 3) 
      document.write(": You lose!"); 
      else if (theRanNum == 2 && myGuess == 1) 
      document.write(": You lose!"); 
      else if (theRanNum == 2 && myGuess == 3) 
      document.write(": You win!"); 
      else if (theRanNum == 3 && myGuess == 1) 
      document.write(": You win!"); 
      else if (theRanNum == 3 && myGuess == 2) 
      document.write(": You lose!"); 
     } 
    } else { 
     document.write("<br>"); 
     document.write("You chose a wrong number!"); 
    } 
</script> 
+0

你可以投我的帖子,帮助你解决问题Tstanley12,但也许你没有得到Stackoverflow的精神,就像你不明白推动。 – prelite

+0

对不起?我在我的回答中合法地记录了你,我只是说我改变了一些东西为我工作,因为我仍然在学校,而且我不会使用我还没有了解的东西。我感谢并记录你,如果那不是好的'精神',那么抱歉... – Tstanley12