如何计算平均值?

时间:2019-01-21 15:17:59

标签: vb.net

因此对于我的代码,我试图计算用户必须输入7个数字的平均值,并且我希望在输入数字后计算平均值。我知道平均值是所有数字的总和除以有多少个数字,但我不知道如何将其实际放入我的代码中

这是我的代码:

    Dim output As Integer
    If Not Integer.TryParse(InputTextbox.Text, output) Then
        MessageBox.Show("ERROR! Data must be a whole number")
    Else
        UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
    End If
    InputTextbox.Text = String.Empty
    If UnitsTextbox.Lines.Length >= 8 Then
        EnterButton.Enabled = False

        If UnitsTextbox.Lines.Length >= 8 Then
            InputTextbox.Enabled = False
        End If
    End If
    If output >= 0 AndAlso output <= 1000 Then
        Exit Sub
    Else
        MessageBox.Show("ERROR! Number must be between 0 and 1000!")
    End If
End Sub

预先感谢

〜D

3 个答案:

答案 0 :(得分:2)

我会使用一个ListBox来输出您的数字,以及其他人建议的List(Of Integer)。然后,您可以将DataSource设置为显示数字,如下所示:

Public Class Form1

    Private Units As New List(Of Integer)

    Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
        Dim output As Integer
        If Integer.TryParse(InputTextBox.Text, output) Then
            If output >= 0 AndAlso output <= 1000 Then
                Units.Add(output)
                UnitsListBox.DataSource = Nothing
                UnitsListBox.DataSource = Units
                InputTextBox.Clear()

                If Units.Count = 7 Then
                    InputTextBox.Enabled = False
                    EnterButton.Enabled = False

                    ' ... calculate the average in here! ...
                    Dim sum As Integer
                    ' ... fill in the code ...
                    Dim average As Decimal
                    ' ... fill in the code ...
                    lblAverage.Text = "Average: " & average.ToString("N2")

                Else
                    InputTextBox.Focus()
                End If
            Else
                MessageBox.Show("ERROR! Number must be between 0 and 1000!")
            End If
        Else
            MessageBox.Show("ERROR! Data must be a whole number")
        End If
    End Sub

End Class

我故意省略了计算平均值的部分;您可以自己解决...

*此外,请注意整个代码在逻辑流程上的差异。

答案 1 :(得分:1)

您在TextBox中有数字列表,但是由于它们位于TextBox的.Text属性中,因此它们是字符串而不是数字。

  1. 第一个工作是将其从TextBox中取出,并将其更改为数字。这是在第一个For Each将每个条目添加到Integer列表中完成的。

  2. Linq现在可以用少量代码来计算平均值。这不一定比随后的旧方法快。

  3. 旧方法会产生相同的结果。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim lst As New List(Of Integer)
    For Each line In TextBox1.Lines
        lst.Add(CInt(line))
    Next
    'The Linq way to get Average
    Dim avg = Aggregate number In lst
                  Into Average(number)
    MessageBox.Show($"The Average using Linq is {avg}")
    'EDIT Thanks to Codexer, Linq even shorter code
    Dim avg1 = lst.Average
    MessageBox.Show(avg1.ToString)

    'The old way
    Dim sum As Integer
    For Each number As Integer In lst
        sum += number
    Next
    'Now that you have the sum you can do the division
    Dim avg2 = sum / lst.Count
    MessageBox.Show($"The Average using the old way is {avg2}")
End Sub

答案 2 :(得分:0)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim strA$() = Nothing, Average%, intT%
    'split the text into lines
    strA = Split(TextBox1.Text, vbNewLine)
    'get array length
    For a = 0 To strA.GetUpperBound(0)
        'get the value of the array and summarize
        intT += CInt(strA(a))
    Next
    Average = intT / strA.Length

End Sub