好的我需要通过msgbox获取数组的大小和数组输入,并在列表框中显示数组列表,然后获取数组列表的平均值。这是我到目前为止的代码:
Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim i, size As Integer
size = Val(InputBox("Please enter array size"))
Dim sequence(size) As Integer
'get array values
i = 0
Do While i < size
sequence(i) = Val(InputBox("Please enter element of array"))
i = i + 1
Loop
i = 0
Do While i < size
lstoutArray.Items.Add(sequence(i))
i = i + 1
Loop
End Sub
答案 0 :(得分:1)
虽然这样的事情会奏效:
Dim lstoutArray As New ArrayList
Dim lstoutCount As Double = 0
Dim size As Double
size = Val(InputBox("Please enter array size"))
For i = 1 To size
lstoutArray.Add(Val(InputBox("Please enter element of array")))
lstoutCount += DirectCast(lstoutArray(lstoutArray.Count - 1), Double)
Next
Dim lstoutAverage As Double = lstoutCount / lstoutArray.Count
从这个例子可以看出,使用arraylist的一个主要缺点是它没有强类型。因此,要使用arraylist中的值,您必须将它们转换为您需要的类型。
List(Of)更容易使用,因为它已经强类型并且具有Average
扩展名:
Dim lstoutArray As New List(Of Double)
Dim size As Double
size = Val(InputBox("Please enter array size"))
For i = 1 To size
lstoutArray.Add(Val(InputBox("Please enter element of array")))
Next
Dim lstoutAverage = lstoutArray.Average