如何在 vb.net 中找到平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13303194/
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
How to find average in vb.net
提问by Syed Abdullah
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 中创建一个程序,以仅使用一个变量作为输入框的值来查找数字的平均值,第二个用于计数的变量应该是负数,但我无法得到准确的答案,这是代码
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
回答by Krofz
use this code... i am still need temp variable because before exit the loop, the value should not in
使用此代码...我仍然需要临时变量,因为在退出循环之前,该值不应该在
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
回答by Shiraj Momin
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

