2012-05-29 38 views
1

我有一個名爲txtBox1的文本框,名爲txtbox2的第二個文本框,一個標籤和一個按鈕。我需要創建一個函數多維數組作業

  1. 接受2個整數參數並返回一個字符串。
  2. 基於在這些參數中傳遞的整數創建2維數組。第一個整數參數代表txtbox1,第二個整數參數代表txtbox2。
  3. 使用嵌套的for循環,以填補遞增值的陣列元素以1
  4. 開始作爲你的循環結構的一部分,跟蹤所有元素值的在一個字符串變量,並將它們用逗號分隔。例如,如果用戶在進入txtbox2 3 txtbox1和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 

回答

0

好了,你幾乎擁有了。我假設你只能在#4卡住。請原諒我是否輸入了錯誤的語法,我沒有在很長的時間內完成VB,而且我正在從內存中完成這項工作。

,讓我們來看看你有什麼:

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

在這裏,您正在使用counter來填充增量值的數組。你可以使用這個變量在另一個字符串變量,只是它們之間添加逗號:

第一:一個字符串變量添加到頂部:

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 

然後,使用該變量在循環:

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

最後,改變你的return到數組中發送output不是元素的

return output 

如果要格式化字符串以便不顯示最後一個「,」,請查看Strings.Left函數。它會像這樣:

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

好吧,希望有所幫助。隨意要求澄清或什麼。

+0

非常感謝你澄清它的工作原理,但是當使用輸出時,它從2開始,而不是從1開始拾取字符串?......任何想法爲什麼? – developthestars

+0

找出計數器變量取= 1。感謝你的幫助! – developthestars

+0

ops,是的,你說得對。我會編輯答案是正確的。很高興我能幫上忙。 – gunr2171