2012-01-17 67 views
1

我目前正在研究一个随机数生成器脚本,它将生成三个数字。前两个数字将用于通过引用行和列来选择“表”中的“单元格”。每个单元格内有两个数字可供选择。第三个号码将决定选择哪两个号码。我已经找出了大部分,但我在最后一部分遇到问题。从嵌套数组中打印Javascript

我的“解决方案”是创建二十个数组,每个数组中有二十多个数组。这是第一个阵列的一个例子:

在标题我有以下:

脚本来生成所述第一两个数字:

var randOne20 = Math.floor(Math.random()*20); 
var randTwo20 = Math.floor(Math.random()*20); 

这里是20个阵列的第一行:

var lvl1Roll1 = [[ 1,0 ],[ 1,1 ],[ 1,2 ],[ 1,3 ],[ 1,4 ],[ 1,5 ],[ 1,6 ],[ 1,7 ],[ 1,8 ],[ 1,9 ],[ 1,10 ],[ 1,11 ],[ 1,12 ],[ 1,13 ],[ 1,14 ],[ 1,15 ],[ 1,16 ],[ 1,17 ],[ 1,18 ],[ 1,19 ]]; 

第二行是“lvl1Roll2”,第三是“lvl1Roll3”等一路攀升到“lvl1Roll20。”

在身体,我有以下脚本:

var randOne = randOne20 + 1; 

document.write(window['lvl1Roll'+randOne][randTwo20]); 

的randOne变量来选择相应的行。

我可以弄清楚如何选择特定的单元格(使用randTwo20变量),但我不知道如何选择每个单元格中的第一个或第二个数字。

现在,只是为了澄清,我没有列出第三个随机数发生器代码,因为此刻我只是想弄清楚如何选择每个单元格中的第一个或第二个数字。一旦我明白了,我可以使用一个if/else语句。

此外,如果我不想打印出数字,但将其选作变量,我该怎么做?

谢谢你的帮助!

保重,有一个伟大的日子....

的Ciao, 约翰。

+0

如何在表达式中添加另一个方括号索引? – Pointy 2012-01-17 00:18:28

回答

2

我真的不确定你想要做什么,但...获取所述第一阵列的第二元件的是作为arr[0]arr[1]一样简单,像这样:

// First: 
document.write(window['lvl1Roll'+randOne][randTwo20][0]); 

// Second: 
document.write(window['lvl1Roll'+randOne][randTwo20][1]); 

为了尽可能之前随机化,只要按照相同的模式,但使用Math.round代替Math.floor并且不通过乘20:

var randOne20 = Math.floor(Math.random()*20); 
var randTwo20 = Math.floor(Math.random()*20); 
var randOne = Math.round(Math.random()); 

document.write(window['lvl1Roll'+randOne20][randTwo20][randOne]); 

但是还有一件事:您不需要将不同的行放在单独的变量中。你可以只让一个大阵列:

var lvl1Roll = [ 
    [ 
     [ 1,0 ], [ 1,1 ], [ 1,2 ], [ 1,3 ], [ 1,4 ], [ 1,5 ], [ 1,6 ], [ 1,7 ], [ 1,8 ], [ 1,9 ], [ 1,10 ], [ 1,11 ], [ 1,12 ], [ 1,13 ], [ 1,14 ], [ 1,15 ], [ 1,16 ], [ 1,17 ], [ 1,18 ], [ 1,19 ] 
    ], 
    // [ lvl1Roll2 ], 
    // [ lvl1Roll3 ], etc. 
]; 

并使用,然后用它来从表中选择一个值,并把它放入变量result

var result = lvl1Roll[randOne20][randTwo20][randOne]; 

最后:我怀疑,我要告诉我们“滚动”背后的逻辑,你根本不需要这个“表”。我可能是错的,但是......可能值得发布关于该问题的另一个问题吗?只是一个想法。

+0

谢谢大家的回复。 PPvG,arr [0]/arr [1]正是我所需要的。我必须有一个巨大的大脑放屁,因为我认为我尝试过,并没有得到我期待的结果。我只是试了一下,它运行良好。谢谢! 关于随机函数,我应该提到我正在生成1到20之间的随机数。 – Mock26 2012-01-17 01:38:22

+1

(也许你试过'[1]'和'[2]'?)给我打电话,但是.. 。不会'1 + Math.floor(Math.random()* 20)'完全符合你的需求?它会生成一个介于1和20(包括)之间的随机数。 – PPvG 2012-01-17 01:59:42

0

如果你想两个数字(假设0和1)之间的随机选择,然后:

var i = Math.random() < 0.5? 0 : 1; 

var i = Math.random()*2 | 0; 

应该做的工作。所以:

document.write(window['lvl1Roll'+randOne][randTwo20][i]); 

document.write(window['lvl1Roll'+randOne][randTwo20][Math.random() < 0.5? 0 : 1]); 

document.write(window['lvl1Roll'+randOne][randTwo20][Math.random()*2 | 0); 

多少你想要什么? :-)

+0

或者只是'Math.round(Math.random())' – PPvG 2012-01-17 00:21:33

0

如果我正确理解你,我认为最简单的方法是使用一个包含多个数组的数组。第一个将是table阵列,table阵列中的每个条目将是row,并且row中的每个条目将是cell。这将使得引用表中的任何单元格变得非常简单。这里是一个3x3例如:

var table = [ 
    [0,1,2], 
    [3,4,5], 
    [6,7,8], 
]; 

table[0][0]; // First Row, First Column, Equals 0; 
table[1][1]; // Second Row, Second Column, Equals 4; 
table[2][2]; // Third Row, Third Column, Equals 8; 

其实你可以把你的20个阵列到阵列中创建表,如果你想:

var table = []; 
table.push(randOne20); 
table.push(randTwo20); 
etc... 

如果要自动加载随机阵列数字你可以很容易地做到这一点循环。

var table = []; 
for (var i = 0; i < 20; i++){ // Create 20 rows 
    var row = []; 
    for (var x=0; i<20; i++){ // Create 20 cells; 
     var cell = [Math.round(Math.random), Math.round(Math.random)]; // Adds a two item array of random 0's or 1's 
     row.push(cell); // Adds the cell to the row 
    } 
    table.push(row); // Adds the row to table 
}