0

我需要基于变量 Y 创建一个特定的数组(我在一个系列中使用)。这是我正在使用的格式 - 尝试在 VBScript 中进行。

If Y=2 then

    S1 = [0,x]
    S2 = [x,0]

If Y=3 then

    S1 = [0,0,x]
    S2 = [0,x,0]
    S3 = [x,0,0]

If Y=4 then

    S1 = [0,0,0,x]<br/>
    S2 = [0,0,x,0]<br/>
    S3 = [0,x,0,0]<br/>
    S4 = [x,0,0,0]<br/>

If Y=5 then

    S1 = [0,0,0,0,x]
    S2 = [0,0,0,x,0]
    S3 = [0,0,x,0,0]
    S4 = [0,x,0,0,0]
    S5 = [x,0,0,0,0]

等等等等。我知道这将是一个 for 循环 -------------

for i = 1 to Y
    S[i] = "[.... this is where am drawing a brain freeze
next
4

1 回答 1

0

您可以使用 arraylists 动态创建您的数组并将它们传输回 vbscript 数组以使其可用于其他函数。(或者您可以继续使用在我看来优于普通数组的数组列表。)

Option Explicit

' Initialize
Dim x : x = "X"
Dim Y : Y = 5
Dim AL : Set AL = CreateObject("System.Collections.ArrayList")
Dim i, j, innerAL, S

' Make a loop to iterate over the amount of array entries we have to create
for i = 1 to Y

    ' This arraylist is used to set the inner array
    Set innerAL = CreateObject("System.Collections.ArrayList") 

    ' Fill the inner array
    for j = 0 to Y-1

        ' See if it has to be filled with X or 0
        If j = (Y-i) then
            innerAL.Add x
        else
            innerAL.Add 0
        end if
    next

    ' Convert the inner arraylist to a standard array and add it to the outer arraylist
    AL.Add innerAL.ToArray()

next

' We are done. Convert the arraylist to a standard array
S = AL.ToArray()

' Display the results
for i = 0 to Y-1
    wscript.echo "S(" & i & ") = [" & join(S(i), ",") & "]"
Next
于 2012-12-04T15:59:34.847 回答