【问题标题】:how do i get a new number to generate我如何获得一个新号码来生成
【发布时间】:2019-12-09 12:52:38
【问题描述】:

我需要制作一个掷骰子的程序,它会掷骰子并说明谁赢了,如果是平局,它会再次掷骰子,每次获胜后,你首先获得一分,只要我运行我的游戏,它就会使用相同的数字。一遍又一遍,因为它只生成了一次我该如何解决这个问题以及在这个问题之后我还需要做什么才能完成程序,感谢您的帮助!

    <script>

var comp1 = Math.floor((Math.random()*6) + 1);
var comp2 = Math.floor((Math.random()*6) + 1);
var you1 = Math.floor((Math.random()*6) + 1);
var you2 = Math.floor((Math.random()*6) + 1);
var counter = 1;
var youPoints = 0;
var mePoints = 0;


while(counter < 6)
{{
    alert("Let's shake some dice!")

alert("your turn to roll \n\n you shook a " + you1 + " and a " +  you2 + ", so you have " + (you1 + you2));

    alert("my turn to roll \n\n I shook a " + comp1 + " and a " +  comp2 + ", so I have " + (comp1 + comp2));
    counter++

    var you = you1 + you2;
    var me = comp1 + comp2;
    if(you > me)
    {
        alert("you win " + you + " to " + me);
        youPoints++
    }
    if (me > you)
    {

        alert("I win " + me + " to " + you);
        mePoints++

    }


}}




</script>

【问题讨论】:

    标签: javascript while-loop


    【解决方案1】:

    您正在 while 循环之外初始化随机变量 (you1, you2)。

    它只被初始化一次,因此每次都产生相同的数字。

    将它带入循环,这可能会解决它!

    【讨论】:

      【解决方案2】:

      将生成随机数的代码移到循环内部,因为现在它们只生成一次……甚至在循环开始之前。

      另外,请帮自己一个忙,使用for 计数循环,而不是while,因为while 循环很容易被错误配置而导致发生无限循环。

      var youPoints = 0;
      var mePoints = 0;
      
      for(var counter = 1; counter < 6; counter++){
        // The code that generates the random numbers has to be in the loop
        // in order for new randoms to be generated upon each loop iteration
        var comp1 = Math.floor((Math.random()*6) + 1);
        var comp2 = Math.floor((Math.random()*6) + 1);
        var you1 = Math.floor((Math.random()*6) + 1);
        var you2 = Math.floor((Math.random()*6) + 1);
        
        alert("Let's shake some dice!")
        alert("your turn to roll \n\n you shook a " + you1 + " and a " +  you2 + ", so you have " + (you1 + you2));
        alert("my turn to roll \n\n I shook a " + comp1 + " and a " +  comp2 + ", so I have " + (comp1 + comp2));
      
        var you = you1 + you2;
        var me = comp1 + comp2;
        
        // Don't make two separate if statements. Use one with an else if
        if(you > me) {
          alert("you win " + you + " to " + me);
          youPoints++
        } else if (me > you) {
          alert("I win " + me + " to " + you);
          mePoints++
        }
      }

      【讨论】:

        【解决方案3】:

        给你,这应该是一个完整的工作示例。

        注意:我将alert() 替换为console.log(),这样我们就可以在控制台中看到此处的输出并且没有弹出窗口,但无论哪种方式都可以。

        var compPoints = 0;
        var youPoints = 0;
        var winnerOfFive = false;
        
        function rollTheDice() {
          return Math.floor((Math.random()*6) + 1);
        }
        
        function rollAllDice() {
          let you1 = rollTheDice();
          let you2 = rollTheDice();
          let comp1 = rollTheDice();
          let comp2 = rollTheDice();
        
          console.log("your turn to roll \n\n you shook a " + you1 + " and a " +  you2 + ", so you have " + (you1 + you2));
          console.log("my turn to roll \n\n I shook a " + comp1 + " and a " +  comp2 + ", so I have " + (comp1 + comp2));
        
          var you = you1 + you2;
          var me = comp1 + comp2;
        
          if(you > me) {
            console.log("you win " + you + " to " + me);
            youPoints++;
          } else if(me > you) {
            console.log("I win " + me + " to " + you);
            compPoints++;
          } else {
            console.log("It was a tie, no one wins. Re-rolling...");
            rollAllDice();
          }
        }
        
        function startGame() {
          while( !winnerOfFive ) {
            console.log("Let's shake some dice!")
            rollAllDice();
        
            if(compPoints == 5) {
              console.log("Comp is first to 5 games and wins " + compPoints + " to " + youPoints);
              winnerOfFive = true;
            } else if (youPoints == 5) {
              console.log("You are first to 5 games and win " + youPoints + " to " + compPoints);
              winnerOfFive = true;
            }
          }
        }
        
        // Start the game like this
        startGame();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-13
          • 2014-06-11
          • 2022-01-15
          • 1970-01-01
          • 2010-09-07
          • 2021-02-28
          • 1970-01-01
          • 2019-10-20
          相关资源
          最近更新 更多