如何通过按钮事件查找动态数组中的最大和最小数字

时间:2015-12-15 16:09:01

标签: vb.net

我正在尝试获取按钮btnMaximum和btnMinimum以向标签显示正确的最大和最小数字。每个按钮都是一个单独的按钮,单击时将显示最大或最小数字。

我已经有了阵列,但我不知道找到Max和Min的正确功能。

这是我到目前为止所做的:

Public Class Form1

Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
    Dim intNumbers(-1) As Integer
    Dim intCounter As Integer
    ReDim intNumbers(14)

    For intCounter = 0 To intNumbers.Length - 1
        intNumbers(intCounter) = Int((100 - 1 + 1) * Rnd()) + 1
        Me.lstNumbers.Items.Add(intNumbers(intCounter))
    Next
    ReDim Preserve intNumbers(14)
    lstNumbers.Items.Clear()
    For intCounter = 0 To intNumbers.Length - 1
        Me.lstNumbers.Items.Add(intNumbers(intCounter))
    Next


End Sub

Private Sub btnMaximum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximum.Click


End Sub

Private Sub btnMinimum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinimum.Click

End Sub
End Class

我真的很感激帮助。

1 个答案:

答案 0 :(得分:3)

首先,您需要将整数数组的声明移到btnGenerate的click事件之外。这将允许您在该特定函数之外访问数组(IE:在最小和最大按钮函数内)。

数组有两个方便的函数,MinMax

Dim intNumbers() As Integer = New Integer() {0, 1, 2, 3, 4, 5}
Dim intMin As Integer = intNumbers.Min          'will contain value 0
Dim intMax As Integer = intNumbers.Max          'will contain value 5