vb平均值的求解
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'这段代码的功能是计算textbox2中的数字的平均值。
Dim Varle(10) As Integer'每一个元素之值
Dim VarleChr(10) As String'每一个元素的字符
Dim i As Integer'循环变量
Dim Av As Single'平均值
Dim totals As Integer'元素值总和
Dim lines As Integer'行数,或者元素个数
VarleChr = Split(TextBox2.Text, vbCrLf) '检出每一行的字符
lines = UBound(VarleChr)
For i = 0 To lines '计算平均值
Varle(i) = Val(VarleChr(i))
totals = Varle(i) + totals
Next
Av = totals / i
Label1.Text = "平均值=" + Str(Av) '用label控件输出结果比较好哦!
End Sub
'这是用VB2005编辑通过的,不知你那边能通过否。
Private Sub Command1_Click()
'添加数值
If Text2.Text <> "" Then
Text2.Text = Text2.Text & vbCrLf & Text1.Text
Else
Text2.Text = Text1.Text
End If
End Sub
Private Sub Command2_Click()
'计算平均
Dim v() As String
Dim i, avg As Integer
v() = Split(Text2.Text, vbCrLf)
i = 0
For u = 0 To UBound(v)
avg = avg + v(i)
i = i + 1
Next
avg = avg / i
Text2.Text = Text2.Text & vbCrLf & avg
End Sub
我的代码在VB 6.0中可以通过的
你是.net环境没试过哦