如何在Visual Basic代码中计算平均值?

时间:2019-01-31 04:11:32

标签: vb.net forms

所以我只是想知道如何在视觉基本代码中计算平均值?

我当前已创建一个表单,用户要输入6个课程的6个编号,并且它们必须在文本框中。我知道平均值是将6个数字相加后除以计数,但我不知道如何从文本框中获取数字以计算平均值。

我已经在网上搜索了此问题的答案,但与该确切问题无关。我的教科书也没有帮助。

任何帮助将不胜感激。

Dim input As Integer
    If Integer.TryParse(InputTextbox1.Text, input) Then

      If input >= 92 And input <= 100 Then
            OutputTextbox1.Text = "A+"

       ElseIf input >= 88 And input <= 91 Then
            OutputTextbox1.Text = "A"
       ElseIf input >= 85 And input <= 87 Then
            OutputTextbox1.Text = "A-"

       ElseIf input >= 82 And input <= 84 Then
            OutputTextbox1.Text = "B+"

       ElseIf input >= 78 And input <= 81 Then
            OutputTextbox1.Text = "B"

       ElseIf input >= 75 And input <= 77 Then
            OutputTextbox1.Text = "B-"

       ElseIf input >= 72 And input <= 74 Then
            OutputTextbox1.Text = "C+"

       ElseIf input >= 68 And input <= 71 Then
            OutputTextbox1.Text = "C"

       ElseIf input >= 65 And input <= 67 Then
            OutputTextbox1.Text = "C-"

       ElseIf input >= 55 And input <= 64 Then
            OutputTextbox1.Text = "D"

       ElseIf input <= 54 Then
            OutputTextbox1.Text = "F"
        End If
    Else
       ErrorTextbox.Text = "Please ensure that what you input is a number between 0 and 100"

    End If

这是我目前拥有的代码,上面有6个文本框,使用上述代码将数字转换为字母。用户输入的数字是我需要计算的平均值。

谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下: 我要做的第一件事是我将所有文本框的数量总计,然后除以总数,这样我就可以获得平均值

注意:不允许文本框输入字母,因为它会出错 我将文本框文本转换为两倍,以便将其视为数字而不是字母。

Public Class Form4
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim average As Double = 0.0
            Dim total As Double = 0.0
            total = CDbl(TextBox1.Text) + CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + CDbl(TextBox4.Text) + CDbl(TextBox5.Text) + CDbl(TextBox6.Text)
            average = total / 6
            TextBox7.Text = average.ToString()
        End Sub
    End Class

enter image description here

enter image description here

已修改:label_grade是等级字母

Public Class Form4
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim average As Double = 0.0
        Dim total As Double = 0.0
        total = CDbl(TextBox1.Text) + CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + CDbl(TextBox4.Text) + CDbl(TextBox5.Text) + CDbl(TextBox6.Text)
        average = total / 6
        TextBox7.Text = average.ToString()


        If average >= 92 And average <= 100 Then
            label_Grade.Text = "A+"

        ElseIf average >= 88 And average <= 91 Then
            label_Grade.Text = "A"
        ElseIf average >= 85 And average <= 87 Then
            label_Grade.Text = "A-"

        ElseIf average >= 82 And average <= 84 Then
            label_Grade.Text = "B+"

        ElseIf average >= 78 And average <= 81 Then
            label_Grade.Text = "B"

        ElseIf average >= 75 And average <= 77 Then
            label_Grade.Text = "B-"

        ElseIf average >= 72 And average <= 74 Then
            label_Grade.Text = "C+"

        ElseIf average >= 68 And average <= 71 Then
            label_Grade.Text = "C"

        ElseIf average >= 65 And average <= 67 Then
            label_Grade.Text = "C-"

        ElseIf average >= 55 And average <= 64 Then
            label_Grade.Text = "D"

        ElseIf average <= 54 Then
            label_Grade.Text = "F"
        End If
    End Sub