VB程序设计题目!求一组数中所有偶数元素的平均值,要求:(1)编制一个自定义函数过程Even,计算数组中所有偶数元素的平

1个回答

  • Private Function Even(intData() As Integer) As Long

    Dim i As Long, sum As Long, ic As Long

    sum = 0

    ic = 0

    For i = LBound(intData) To UBound(intData)

    If ((intData(i) Mod 2) = 0) Then

    sum = sum + intData(i)

    ic = ic + 1

    End If

    Next

    Even = sum / ic

    End Function

    Private Sub Form_Click()

    Dim intData(9) As Integer, i As Integer

    Randomize

    Me.Cls

    For i = 0 To 9

    intData(i) = 1 + CInt(99 * Rnd)

    Next

    Me.Print Even(intData)

    End Su