VB.net 3 个数字的平均值两种不同的方式。初学者问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/632988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:07:18  来源:igfitidea点击:

VB.net Average of 3 numbers two different ways. Beginners question

vb.netaverage

提问by Davey

I have a form that has 3 text boxes for 3 input values, along with a list box for output. I need the user to be able to input 3 different numbers and click a button to find the average. I'm not really sure how to do/approach this. Any help is greatly appreciated.

我有一个表单,其中包含 3 个用于 3 个输入值的文本框,以及一个用于输出的列表框。我需要用户能够输入 3 个不同的数字并单击一个按钮来找到平均值。我不太确定如何做/处理这个。任何帮助是极大的赞赏。

Still Stuck....

还是卡住了....

Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)     
Handles btnAverage.Click
    Dim a As Integer = CInt(txtone.Text)
    Dim b As Integer = CInt(txtTwo.Text)
    Dim c As Integer = CInt(txtThree.Text)
    Dim average As Integer
    average = (a + b + c) / 3
    lstOutput.Text = average

回答by JaredPar

Are you unsure about how to convert the input to numbers? If so use the CInt function.

您不确定如何将输入转换为数字?如果是这样,请使用 CInt 函数。

Public Sub OnAverageClick(ByVal sender as Object, ByVal e As EventArgs) Handles AverageButton.Click

    Dim input1 as Integer = CInt(textBox1.Text)
    Dim input2 as Integer = CInt(textBox2.Text)
    Dim input3 as Integer = CInt(textBox3.Text)
    Dim average = (input1 + input2 + input3) / 3

End Sub

回答by Daniel A. White

@JaredPar

@JaredPar

I would use Integer.TryParse instead.

我会改用 Integer.TryParse 。

回答by Jhollman

This function calculates the Average of any number of non-zero values:

此函数计算任意数量的非零值的平均值:

''' <summary>Calcula el Promedio de los Valores ingresados.
''' Sólo tiene en cuenta los Valores mayores que 0.</summary>
''' <param name="diasValores">Valores a Calcular</param>
Function PromedioValores(ByVal ParamArray diasValores() As Integer)
    'Esta funcion calcula el promedio de los valores ingresados como parametro
    Dim result As Double = 0
    If diasValores.Length <= 0 Then Exit Function
    Dim cant As Integer = 0
    For i As Integer = 0 To UBound(diasValores, 1)
        If diasValores(i) > 0 Then
            cant = cant + 1
            result += diasValores(i)
        End If
    Next i
    If result > 0 Then
        result = result / cant
    End If

    Return result
End Function

Use:

用:

Me.TextBox1.Text = PromedioValores(10, 0, 0, 15, 0, 12, 12, 0)

回答by Element

protected sub on_btn_click()

listbox1.items.add(new listitem((integer.parse(textbox1.text) + integer.parse(textbox2.text) + integer.parse(textbox3.text)) / 3 ))

end sub