How to calculate average in visual basic code?

☆樱花仙子☆ 提交于 2020-01-06 09:03:42

问题


So I was just wondering how to calculate an average in visual basic code?

I currently have a form created and the user is to enter 6 numbered for 6 courses and they must be in textboxes. I know that average is the 6 numbers added together divided by the count but I don't know how to grab the numbers from the textbox to calculate the average.

I've searched online for the answer to this but nothing pertains to this exact problem. My textbook is also of no help.

Any help would be greatly appreciated.

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

This is the code I have currently there is 6 textboxes using the above code to transfer numbers to letters. The numbers that the user enters is what i need to calculate into the average.

Thanks


回答1:


Try this: first thing i did is that i total all textbox numbers and then divide to total number so that i can get the average

Note: dont allow textbox to input a letters because it will error i converted the textbox text to double so that it will consider as number and not letters.

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

Modified: The label_grade is the letter of the 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


来源:https://stackoverflow.com/questions/54453146/how-to-calculate-average-in-visual-basic-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!