简体   繁体   English

滚动3次死亡并将每个存储在一个数组中并以html显示

[英]Roll 3 die 50 times and store each in an array and display in html

I need to simulate rolling 3 die 50 times, storing each in an array, and displaying the results in a table. 我需要模拟滚动3个模具50次,将每个模具存储在一个数组中,并将结果显示在表格中。 I also need to display the results of each instance added together. 我还需要显示添加在一起的每个实例的结果。 Ex on roll 1 for each I got 3+4+2 = 9. I have some code, but it won't display. 每卷第1卷,我得到3 + 4 + 2 = 9.我有一些代码,但它不会显示。 I'm not sure what's wrong with it. 我不确定它有什么问题。

  <!--
  var array1 = new Array(50);
  var array2 = new Array(50);
  var array3 = new Array(50);
  var array4 = new Array(50);

  function roll()
  {
    var face = Math.floor( (Math.random() *6) + 1);
    return face;
  }

  for( var i = 0; i < array.length; i++)
  {
     array1[i] = roll();
  }

  for( var i = 0; i < array.length; i++)
  {
     array2[i] = roll();
  }

  for( var i = 0; i < array.length; i++)
  {
     array3[i] = roll();
  }

  for( var i = 0; i < array.length; i++)
  {
     array4[i] = array1[i] + array2[i] + array3[i];
  }

  //Making table
  document.write('<table border="1" cellspacing="1" cellpadding="5">')

  //Loops through array object and writes values in appropriate table cells
  for(var i = 0; i < 50; i++)
  {
    document.write('<tr>')
    document.write('<td>' + i + '</td>');
    document.write('<td>' + array1[i] + '</td>');
    document.write('<td>' + array2[i] + '</td>');
    document.write('<td>' + array3[i] + '</td>');
    document.write('<td>' + array4[i] + '</td>');
    document.write('</tr>')
  }

  document.write('</table>')

You have an error in for( var i = 0; i < array.length; i++) each time. 每次都有for( var i = 0; i < array.length; i++) replace them with for( var i = 0; i < array1.length; i++) , for( var i = 0; i < array2.length; i++) , for( var i = 0; i < array3.length; i++) and for( var i = 0; i < array4.length; i++) 替换for( var i = 0; i < array1.length; i++)for( var i = 0; i < array2.length; i++)for( var i = 0; i < array3.length; i++)for( var i = 0; i < array4.length; i++)

Your array is not defined in each of the for statements. 您的array未在每个for语句中定义。 Change them accordingly: 相应地更改它们:

for( var i = 0; i < array1.length; i++)
{
    array1[i] = roll();
}

for( var i = 0; i < array2.length; i++)
{
    array2[i] = roll();
}

for( var i = 0; i < array3.length; i++)
{
    array3[i] = roll();
}

for( var i = 0; i < array4.length; i++)
{
    array4[i] = array1[i] + array2[i] + array3[i];
}

Also, you're missing a bunch of semicolons in your code. 此外,您在代码中缺少一堆分号。 Here's a working JSFiddle of your implementation. 这是一个有效的JSFiddle实现。

Reading your browser's developer tool's console will have helped you notice the error in question. 阅读浏览器的开发人员工具控制台将帮助您注意到有问题的错误。

Here's another way to possibly implement it: JSFiddle 这是另一种可能实现它的方法: JSFiddle

I had fun with this, and made a more generalized version for many dice: JSFiddle 我玩得很开心,为许多骰子制作了一个更通用的版本: JSFiddle

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

相关问题 带有 6 个模具图像的 JS 模具滚动模拟 - JS die roll simulation with 6 die images 如何更新我的骰子 animation 以便它从每次随机滚动后结束的位置开始? - How to update my die animation so that it starts from where it ended after each random roll? 尝试显示使用数组选择每个选项的次数 - Trying To Display Number Of times each option is selected using an array 如何多次从数组中随机选择一个对象并将每个对象存储在一个数组中? - How do I randomly select an object from an array multiple times and store each in an array? 我如何正确地使用“ for循环”在一个骰子功能中滚动一个(或多个)骰子? - How do I properly use a “for loop” to roll one die (or multiple) within a function of another die roll? 使用for循环将html元素的值存储到数组中并显示在另一个元素中 - using for loop to store value of html element into an array and display in another element 如何将JSON数据存储在数组中,然后在HTML页面上显示 - How to store the JSON data in an array and then display it on the HTML page 显示模具(“消息”)问题 - Display die('message') problem 遍历 JSON 并在 HTML 表格中显示每个数组项 - Looping through JSON and display each array item in HTML table 将$ .each结果存储到数组中? - store $.each result into array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM