1

我有一个名为 txtBox1 的文本框、一个名为 txtbox2 的第二个文本框、一个标签和一个按钮。我需要创建一个函数

  1. 接受 2 个整数参数并返回一个字符串。
  2. 根据传入这些参数的整数创建一个二维数组。第一个整数参数将代表 txtbox1,第二个整数参数将代表 txtbox2。
  3. 使用嵌套的 for 循环以从 1 开始的递增值填充数组元素
  4. 作为循环结构的一部分,跟踪字符串变量中的所有元素值并用逗号分隔它们。例如,如果用户在 txtbox1 中输入 3,在 txtbox2 中输入 5 并单击按钮,我们将得到一个如下所示的数组:

    :Length=15
    (0,0): 1    
    (0,1): 2
    (0,2): 3
    (0,3): 4
    (0,4): 5
    (1,0): 6
    (1,1): 7
    (1,2): 8
    (1,3): 9
    (1,4): 10 
    (2,0): 11
    (2,1): 12
    (2,2): 13
    (2,3): 14
    (2,4): 15    
    

    元素中填充的值将是 1,2,3,4,5,6,7,8,9,10,11,12,13,14 和 15。

  5. 传回的字符串格式为“数组为 3 x 5,数组元素中的值为 1,2,3,4,5,6,7,8,9,10,11,12,13,14 ,15”。
  6. 使用此字符串值填充标签。

这是我到目前为止...

Shared Function myArray(int1 As Integer, int2 As Integer) As String

    Dim Array(int1 - 1, int2 - 1) As Integer
    Dim i As Integer
    Dim j As Integer
    Dim counter As Integer = 1

    For i = 0 To int1 - 1
        For j = 0 To int2 - 1
            Array(i, j) = counter
            counter += 1
        Next
    Next

    Return Array(i, j)

End Function
4

1 回答 1

0

Ok, so you almost had it. I am assuming you are stuck on only #4. And please excuse me if I type the incorrect syntax, I have not done VB in a LONG time and I am doing this from memory.

let's take a look at what you had:

For i as integer = 0 To int1 - 1
    For j as integer = 0 To int2 - 1
        Array(i, j) = counter
        counter += 1

Here you are using counter to populate the array with incremental values. You can just use this variable in another string variable, and just add commas between them:

first: add a string variable to the top:

Dim Array(int1 - 1, int2 - 1) As Integer
Dim i As Integer
Dim j As Integer
Dim counter As Integer = 1
Dim output as String = nothing

Then, use that variable in your loop:

For i = 0 To int1 - 1
    For j = 0 To int2 - 1
        Array(i, j) = counter
        output += "" & counter & ", "
        counter += 1
    Next
Next

last, change your return to send output instead of an element in your array

return output

If you want to format the string so that the last ", " is not shown, look at the Strings.Left function. It would go something like this:

output = Left(output, Len(output) - 2) 'I am not sure if this is a 2 or 3, run and test

Ok, hope that helps. Feel free to ask for clarification or something.

于 2012-05-29T16:49:48.817 回答