【问题标题】:Repeating functions in javascript (rock, paper, scissors)javascript中的重复功能(石头,纸,剪刀)
【发布时间】:2014-05-08 18:47:23
【问题描述】:

我是 javascript 和一般编码方面的初学者,我刚刚制作了一个石头、纸、剪刀游戏,您可以在其中重新选择是否平局。 我希望函数在第 15 行之后重复(当然,如果条件为真),但我不知道该怎么做...... 所以如果有平局,你应该重新选择(这可行),但是在重新选择之后,计算机的选择的生成应该重复并且与 compare() 函数相同,以使系统重新选择- 选择工作。

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";
} 

var compare = function(choice1, choice2) {
 if (choice1 === choice2) {
     console.log("The result is a tie, please re-choose!")
     userChoice = prompt("Do you choose rock, paper or scissors?");
 } 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);

【问题讨论】:

    标签: javascript function repeat


    【解决方案1】:

    您可以为选择过程创建一个函数,然后在比较中调用它:

    var userChoice, computerChoice;
    
    var choose = function() {
        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";
        } 
    }
    
    
    var compare = function(choice1, choice2) {
        if (choice1 === choice2) {
            console.log("The result is a tie, please re-choose!")
            choose();                                   // Choose again
            return compare(userChoice, computerChoice); // Compare again, recursively!
        } else if (choice1 === "rock") {
            ...
        }
        ...
    };
    
    choose();                             // Make the choice
    compare(userChoice, computerChoice);  // Compare
    

    或者,如果您不想使用递归,您可以return "tie"; 以防出现平局,然后重复该过程直到平局解决:

    var choose = function() {
        // As above
        ...
    }
    
    var compare = function(choice1, choice2) {
        if (choice1 === choice2) {
            console.log("The result is a tie, please re-choose!")
            return "tie"; // Tie occurred
        }
        ...
    };
    
    
    var result;
    
    do {
        choose();                                     // Choose an item
        result = compare(userChoice, computerChoice); // Compare the choices
    } while(result != "tie");                         // Repeat until ties are resolved
    

    【讨论】:

      【解决方案2】:

      只使用递归:

       if (choice1 === choice2) {
           console.log("The result is a tie, please re-choose!")
           userChoice = prompt("Do you choose rock, paper or scissors?");
           compare( userChoice, choice2 );
       } else if (choice1 === "rock") {
      

      【讨论】:

      • 这不会再次生成计算机选择。 但重新选择后,计算机的选择要重复生成
      【解决方案3】:

      我最近在 CodeAcademy 开始使用 JavaScript 并遇到了同样的问题, John 的回答是正确的,尽管它需要进行一些更正才能起作用。

      这部分,我们调用choice()然后调用compare(userChoice,computerChoice)函数,变量userChoice和computerChoice在choose函数内部声明,使它们成为局部变量,不能从代码的最后一行调用.

      您需要做的唯一更改是您需要在选择函数中调用 compare(userChoice,computerChoice) 然后它就可以正常工作了!

      这是对我有用的完整代码。

         var choice = function()
              {
                      var userChoice = prompt("Select rock, paper, scissors");
                      var computerChoice = Math.random();
      
                      if(computerChoice<=0.33){
                          computerChoice = "rock";
                      }
                      else if(computerChoice<=0.67){
                          computerChoice = "paper";
                      }
                      else{
                          computerChoice = "scissors";
                      }
                      alert("The Computer's choice is:- "+computerChoice);
                      var answer = compare(userChoice,computerChoice);
                      alert(answer);
      
      
              };
      
              var compare = function(choice1,choice2)
              {
                  if(choice1===choice2)
                  {
                      alert("It's a tie! You must select another choice!");
                      choice();
                      return compare(userChoice,computerChoice);
                  }
      
                  else if(choice1==="rock")
                  {
                      if(choice2==="scissors")
                      {
                          return("Rock wins! You've won!");
                      }
                      else
                      {
                          return("Paper wins! You lose!");
                      }
                  }
                  else if(choice1==="paper")
                  {
                      if(choice2==="rock")
                      {
                          return("paper wins! You've won!");
                      }
                      else
                      {
                          return("Scissors wins! You lose!");
                      }
                  }
                  else if(choice1==="scissors")
                  {
                      if(choice2==="paper")
                      {
                          return("Scissors wins! You've won!");
                      }
                      else
                      {
                          return("Rock wins! You lose!");
                      }
                  }
              };
              choice();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-22
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 2014-11-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多