如何在Visual Basic代码中计算平均值? [英] How to calculate average in visual basic code?

查看:293
本文介绍了如何在Visual Basic代码中计算平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

任何帮助将不胜感激.

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个文本框,使用上述代码将数字转换为字母.用户输入的数字是我需要计算出的平均值.

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.

谢谢

推荐答案

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

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

已修改:label_grade是等级字母

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

这篇关于如何在Visual Basic代码中计算平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆