我正在尝试制作六个数组,所以我已经手动创建了它们。我知道有一种方法可以让这整个混乱更短,但它让我的思绪下滑。
var variant_1:Array = new Array
(rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(),
"", "", "", "", "", "", "", "", "", "", "", "");
var variant_2:Array = new Array
(rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(),
"", "", "", "", "", "", "", "", "", "", "", "");
var variant_3:Array = new Array
(rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(),
"", "", "", "", "", "", "", "", "", "", "", "");
var variant_4:Array = new Array
(rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(),
"", "", "", "", "", "", "", "", "", "", "", "");
var variant_5:Array = new Array
(rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(),
"", "", "", "", "", "", "", "", "", "", "", "");
var variant_6:Array = new Array
(rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(),
"", "", "", "", "", "", "", "", "", "", "", "");
注意: getNum()是一个函数,它为我提供1到90之间的随机数字范围,然后我将它们分配给动态创建的表。
注意2 :数组中的空值是表格中的空单元格,因为在9x3的表格中我需要15个数字和12个空格。
注意3 :是的,这是宾果游戏。
谢谢。
答案 0 :(得分:1)
您可以使用for循环,例如
for(var i:int=0; i<10; i++) {
trace(i);
}
对于您要执行的操作,可以将其包装在返回新数组的函数中
function createArray(){
var a = [];//new array
for(var i:int=0; i<??; i++) {
//some conditions
if(i < ??) {
//push to array
}
else {
//push to array
}
}
return a;
}
var myNewArray:Array = createArray();
请查看http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/loops.html有关循环的详细介绍。
答案 1 :(得分:1)
var variants = createVariants(6, 15, 12);
//使用您的变体: // variants [0] [0]与您的variant_1 [0]
相同function createVariants(numVariants : int, numPrefilled : int, numEmpty : int) : Array{
var variants : Array = [];
for(var i : int = 0;i<numVariants;i++){
var variant : Array = [];
for(var j : int = 0;j<numPrefilled ;j++){
variant.push(rp.getNum());
}
for(var j : int = 0;j<numEmpty ;j++){
variant.push("");
}
variants.push(variant);
}
return variants;
}
答案 2 :(得分:1)
基于Paul的答案,您还可以使用二维数组来存储每个数组,这样可以使用循环来创建所需数量的变量(成本会增加必须维护的复杂性)一个2d数组)。
使用2d数组可能是:
function createArrayVarient(){
var result:Array = new Array();
var counter:int;
// 15 as there are 15 numbers added first
for(counter = 0; counter < 15; counter++){
result.push(rp.getNum());
}
// 12 for the number of "" added
for(counter = 0; counter < 12; counter++){
result.push("");
}
return result;
}
var arrayVarients:Array = new Array();
// 6 as you have 6 array variants in your sample
for(var counter = 0; counter < 6; counter++){
arrayVarients.push(createArrayVarient());
}
然后如果您之前没有使用过2d数组,要访问“arrayVarients”中的每个数组变量,它将会出现:
arrayVarients[0] // the first array variant
arrayVarients[1] // the second array variant
arrayVarients[2] // the third array variant
...etc
访问数组变体中的每个值,它将(使用2个索引):
arrayVarients[0][0] // the first value in the first variant array
arrayVarients[2][7] // the eight value in the third variant array
答案 3 :(得分:1)
我想这就是你想要的
var iVariantCount:int = 6;//number of variants
var arrVariants:Array = new Array();//this will hold your variants array (variant_1,variant_2,etc)
var iVariantLength:int = 27;//total no fo elements in a variant array
var iRandomNumCount:int = 15;//no of random numbers in a variant array
for (var i:int = 0; i<iVariantCount ; i++)
{
var arrVariant:Array = new Array();
for (var j:int = 0; j < iVariantCount ; j++)
{
if (i < iRandomNumCount)
{
arrVariant.push(rp.getNum());
}
else
{
arrVariant.push("");
}
arrVariants.push(arrVariant);
}
}
//Check the result
for (i = 0; i<arrVariants.length ; i++)
{
trace("Variant_" + i + ": " + arrVariants[i]);
}
试试这个并告诉我它是否适合你。