Javascript 掷骰子,直到连续两次掷到同一点

Javascript 掷骰子,直到连续两次掷到同一点,javascript,Javascript,我正在做一个关于骰子游戏的作业,这需要编写一个程序来掷骰子,直到它在两个连续的骰子上得到相同的分数,并显示骰子的结果。你能告诉我这个代码有什么问题吗。谢谢大家! <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Roll the dice</title> <script> function hwc() { var roll, score,

我正在做一个关于骰子游戏的作业,这需要编写一个程序来掷骰子,直到它在两个连续的骰子上得到相同的分数,并显示骰子的结果。你能告诉我这个代码有什么问题吗。谢谢大家!

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Roll the dice</title>
<script>
function hwc() {
  var roll, score, scorep, output = "";
  score = Math.floor(Math.random()*6) + 1;
  output = "<table><tr><th>Try time</th><th>Score</th></tr>";
  scorep = score;

  for (roll = 1; roll < 1000; roll = roll + 1) {
    output = output + "<tr><td>" + roll + "</td>";
    score = Math.floor(Math.random()*6) + 1;
    output = output + "<td>" + score + "</td>";
    output = output + "</tr>";

    if (scorep == score) break;

    scorep = score;
  }
  output = output + "</table>";
  document.getElementById("id1").innerHTML = output;
  output = output + "You have rolled the same score on two successive rolls, whose point are " + scorep;
  document.getElementById("id1").innerHTML = output;}
</script>
</head>
<body>
<button3 onclick="hwc()">Part 3 - Roll until the dice get the same score on two consecutive rolls.</button3>
</div>
<p id="id1"></p>
</body>
</html>

掷骰子
函数hwc(){
变量滚动、分数、计分、输出=”;
分数=Math.floor(Math.random()*6)+1;
output=“Try timeScore”;
scorep=分数;
对于(滚动=1;滚动<1000;滚动=滚动+1){
输出=输出+“”+滚动+“”;
分数=Math.floor(Math.random()*6)+1;
输出=输出+“”+分数+“”;
输出=输出+“”;
如果(得分==得分)中断;
scorep=分数;
}
输出=输出+“”;
document.getElementById(“id1”).innerHTML=输出;
输出=输出+“你连续两次掷相同的分数,分数为”+分数;
document.getElementById(“id1”).innerHTML=output;}
第三部分-掷骰子,直到骰子在连续两次掷骰中得到相同的分数。


当我运行这个程序时,在某些情况下,它只显示一个滚动。但是,根据上述要求,您必须至少滚动两次。你能告诉我这个代码有什么问题吗。谢谢大家!

注意,for语句之前的第一个赋值实际上是第一个卷,那么,你认为只有一个卷的时候实际上有两个卷,那么有没有办法显示所有卷?是的,像在for循环中那样将第一个卷写入表中,或者干脆取消初始赋值,这样所有卷都从for循环开始。现在一切都好了。非常感谢。您将自己的掷骰次数限制为999次-虽然概率很低,但您仍有可能在不使用双音符的情况下掷骰999次。for语句之前的第一个赋值实际上是第一次掷骰,那么,你认为只有一个卷的时候实际上有两个卷,那么有没有办法显示所有卷?是的,像在for循环中那样将第一个卷写入表中,或者干脆取消初始赋值,这样所有卷都从for循环开始。现在一切都好了。非常感谢你。你把自己限制在999卷-虽然概率很低,但你仍然有可能在没有双打的情况下掷999卷
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Roll the dice</title>
<script>
function hwc() {
  var roll, score, scorep, output = "";
  score = Math.floor(Math.random()*6) + 1;
  output = "<table><tr><th>Try time</th><th>Score</th></tr>";
  scorep = score;

  for (roll = 1; roll < 1000; roll = roll + 1) {
    output = output + "<tr><td>" + roll + "</td>";
    score = Math.floor(Math.random()*6) + 1;
    output = output + "<td>" + score + "</td>";
    output = output + "</tr>";

    if (scorep == score) break;

    scorep = score;
  }
  output = output + "</table>";
  document.getElementById("id1").innerHTML = output;
  output = output + "You have rolled the same score on two successive rolls, whose point are " + scorep;
  document.getElementById("id1").innerHTML = output;}
</script>
</head>
<body>
<button3 onclick="hwc()">Part 3 - Roll until the dice get the same score on two consecutive rolls.</button3>
</div>
<p id="id1"></p>
</body>
</html>