需要知道代码重复的原因

时间:2017-01-14 00:47:37

标签: vb.net repeat

Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim EvenNum, EvenNumCount, EvenNumAverage, Number, Result As Integer
    Calculations(EvenNum, EvenNumCount)
    GetInput(Number)
    Output(Result)

End Sub
Sub GetInput(ByRef Number)
    Number = txtInput.Text
End Sub
Sub Calculations(ByRef EvenNum, ByRef EvenNumCount)
    Dim ListedNumbers, lstOutputSize As Integer
    GetInput(lstOutputSize)
    For i As Integer = 0 To lstOutputSize - 1
        ListedNumbers = InputBox("Enter Numbers", "Input")
        lstOutput.Items.Add(ListedNumbers)
    Next
    For i As Integer = 0 To lstOutput.Items.Count - 1
        If (CInt(lstOutput.Items(i)) Mod 2 = 0) Then
            EvenNum += lstOutput.Items(i)
            EvenNumCount += 1
        End If
    Next
End Sub

Function Average(ByRef EvenNumAverage As Integer) As Integer
    Dim EvenNum, EvenNumCount As Integer
    Calculations(EvenNum, EvenNumCount)
    EvenNumAverage = EvenNum / EvenNumCount
    Return EvenNumAverage

End Function

Sub Output(ByRef EvenNumAverage)
    lstOutput.Items.Add(Average(EvenNumAverage))
End Sub

该程序应该从文本框中输入所需数量的数字,以便从输入框输入列表框。

然后应该只获得偶数的平均值,然后将该平均值显示在列表框中。

在它的当前状态下,程序将执行它想要做的事情,它只是重复计算代码。这只发生在按钮过程下添加Output调用语句时。

1 个答案:

答案 0 :(得分:1)

您已拨打Calculations两次

  1. 来自btnCalculate_Click
  2. 来自Average
  3. 调用的Output