简体   繁体   English

随机发生

[英]Math.random reoccurring

I am creating a basic modified Crapps game. 我正在创建一个基本的修改型Crapps游戏。 I am using java script and HTML. 我正在使用Java脚本和HTML。 So It will not look great, but its purpose is to teach me Java Script. 因此,它看起来不太好,但是它的目的是教我Java脚本。

I am still in the beginning stages and have hit a road block. 我仍处于起步阶段,遇到了障碍。 What is supposed to happen is when I click a button this function happens. 应该发生的是,当我单击一个按钮时,就会发生此功能。 My function occurs yet it keep rolling 2 6's and thus the dice show up as 6. 我的功能发生了,但它仍然滚动2 6,因此骰子显示为6。

Everything works except that the 6 keeps reoccurring . 一切正常,除了6不断发生

I went through code and could not find common mistakes- making sure the numbers it should generate is covered, that my bases are covered 6 numbers (1-5) all have a job, my names are correct, etc. I know my function is being called because the image of the dice shows up as 6 every time where I want it to go. 我遍历了代码,找不到常见的错误-确保它应该生成的数字被覆盖,我的基数被覆盖6个数字(1-5)都有工作,我的名字正确,等等。我知道我的函数是之所以被称为是因为骰子的图像每次我要去的地方都显示为6。

 function rolldice()
   {
     dice1=Math.floor(Math.random()*6+0);
        if(dice1=0)
            {
             document.getElementById("dice1").innerHTML="<img src='1.jpg' width='100' height='100' />";
            }
        if(dice1=1)
            {
              document.getElementById("dice1").innerHTML="<img src='2.jpg' width='100' height='100' />";

            }
        if(dice1=2)
            {
              document.getElementById("dice1").innerHTML="<img src='3.jpg' width='100' height='100' />";

            }
        if(dice1=3)
            {
              document.getElementById("dice1").innerHTML="<img src='4.jpg' width='100' height='100' />";

            }
        if(dice1=4)
            {
              document.getElementById("dice1").innerHTML="<img src='5.jpg' width='100' height='100' />";

            }
        if(dice1=5)
            {
             document.getElementById("dice1").innerHTML="<img src='6.jpg' width='100' height='100' />";

            }
dice2=Math.floor(Math.random()*6+0);
    if(dice2=0)
            {
             document.getElementById("dice2").innerHTML="<img src='1.jpg' width='100' height='100' />";
            }
        if(dice2=1)
            {
              document.getElementById("dice2").innerHTML="<img src='2.jpg' width='100' height='100' />";

            }
        if(dice2=2)
            {
              document.getElementById("dice2").innerHTML="<img src='3.jpg' width='100' height='100' />";

            }
        if(dice2=3)
            {
              document.getElementById("dice2").innerHTML="<img src='4.jpg' width='100' height='100' />";

            }
        if(dice2=4)
            {
              document.getElementById("dice2").innerHTML="<img src='5.jpg' width='100' height='100' />";

            }
        if(dice2=5)
            {
             document.getElementById("dice2").innerHTML="<img src='6.jpg' width='100' height='100' />";

            }
 }

You are using = instead of == . 您正在使用=而不是== That means that it will run each of the statements from 1-5. 这意味着它将运行1-5中的每个语句。

if (dice1 = 5){

is the same as 是相同的

dice1 = 5;
if (dice1){

Which will always be true. 这将永远是正确的。

You should also have var before each of your variables when you are assigning the random value, FYI. 在分配随机值FYI时,您还应该在每个变量之前添加var

I would consider shortening all of this code to something like this: 我会考虑将所有这些代码缩短为以下形式:

function rolldice(){
  for (var i = 0; i < 2; i++){
    var roll = Math.floor(Math.random() * 6);
    document.getElementById('dice' + (i + 1)).innerHTML = 
      "<img src='" + (roll + 1) + ".jpg' width='100' height='100' />"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM