简体   繁体   English

如何在vb.net中找到平均值

[英]How to find average in vb.net

Hi i am trying to create a programm in vb.net to find a average of number using only one variable for value with inputbox and second for count number should be negative but i can't get accurate answere here is the code 嗨,我正在尝试在vb.net中创建一个programm,以仅使用一个带有输入框的值作为变量,第二个作为计数值来查找数字平均值,这应该是负数,但我无法得到准确的答案,这里是代码

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim num, count As Integer
    num = InputBox("Please enter number") 'for first entry
    While num > 0    ' here we have to check it that the num is not negative then to start
        num = InputBox("Please enter number")
        num += num
        count += 1             'this will calculate how many times number added    
    End While
    MsgBox("Average is " & num / count)


End Sub

use this code... i am still need temp variable because before exit the loop, the value should not in 使用此代码...我仍然需要temp变量,因为在退出循环之前,该值不应位于

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim num, count As Integer
    count = 0
    num = 0
    While num >= 0    ' here we have to check it that the num is not negative then to start
        Dim temp As Integer
        temp = InputBox("Please enter number")
        If temp < 0 Then
            Exit While
        End If
        count += 1             'this will calculate how many times number added 
        num += temp
    End While
    MsgBox("Average is " & (num / count))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)        Handles Button1.Click
Dim num, count, avg As Integer
num = InputBox("Please enter number") 'for first entry
While num > 0    ' here we have to check it that the num is not negative then to start
    avg += num
    count += 1             'this will calculate how many times number added    
    num = InputBox("Please enter number")
End While
    MsgBox("Average is " & avg / count)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM