0

我正在尝试制作六个数组,所以我都是手工创建的。我知道有一种方法可以让整个混乱变得更短,但它让我失去了理智。

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:是的,这是一个宾果游戏。

谢谢你。

4

4 回答 4

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]);
}

试试这个,告诉我它是否适合你。

于 2013-04-17T10:27:05.150 回答
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了解循环的良好介绍。

于 2013-04-17T09:34:11.140 回答
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());
}

那么如果您以前没有使用过二维数组,要访问“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
于 2013-04-17T09:55:26.377 回答
1
var variants = createVariants(6, 15, 12);

// 使用你的变体: // variables[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;
}
于 2013-04-17T09:49:31.800 回答