我知道这是个愚蠢的问题,但我花了4天时间来解决这个问题。我试图将两个各有10个不同元素的数组加在一起,而不是作为一个总和,但作为一个例子,这只是三个数组,[1,3,5]+[4,7,9}=[4,10,14]。我将两个数组显示在前两个按钮的标签中,而另一个标签则是 "和"。我只是不知道如何将它们相加。这是我的代码。
Public Class Form1
Dim array1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim array2() As Integer = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Dim array3() As Integer = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
For i = 0 To array1.Length - 1
Label1.Text = Label1.Text & "," & array1(i)
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
For i = 0 To array2.Length - 1
Label2.Text = Label2.Text & "," & array2(i)
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles
Button3.Click
End Sub
End Class
如果你想把两个数组中相同位置的两个数字相加,那很容易。
你可以从按钮事件中发送数组。
Button.click事件
Dim list1 As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim list2 As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim Results = Add_Groups(list1.ToArray, list2.ToArray)
Private Function Add_Groups(array1() As Integer, array2() As Integer) As String
'Make sure each group has the same number of elements
If array1.Length <> array1.Length Then Return "Groups aren't equal"
'Set returnvalue
Dim RV As String = ""
'Iterate through each group and add them together and add them to the returnvalue
For I As Integer = 0 To array1.Length - 1
'Add location I from group1 and group 2
Dim Sum = array1(I) + array2(I)
'Add to returnvalue
If RV.Length = 0 Then
RV += Sum.ToString
Else
RV += "," + Sum.ToString
End If
'last five lines could be replaced with
' RV += If(RV.Length = 0, "", ",") + Sum.ToString
Next
'Add brackets
RV = "[" + RV + "]"
Return RV
End Function