求问一道VB题,有关编写平均值函数的
编写函数AverageA,要求能计算数组A(A采用Variant类型)中所有元素的平均值(Double)。再编写一个命令按钮Command1的Click事件,对文本框Te...
编写函数AverageA,要求能计算数组A(A采用Variant类型)中所有元素的平均值(Double)。
再编写一个命令按钮Command1的Click事件,对文本框Text1中输入的以1个空格间隔的数据,调用自定义的函数过程AverageA,最后输出平均值到文本框Text2中。
注意: 不要改变函数名和参数类型。
提示:
将一系列以空格间隔的数据放入数组可以采用以下语句:
Dim A
A = Split(Trim(Text1.Text))
文本框数据以空格为分隔符把数据分割到数组A中,数组下界为0,上界为UBound(A) 展开
再编写一个命令按钮Command1的Click事件,对文本框Text1中输入的以1个空格间隔的数据,调用自定义的函数过程AverageA,最后输出平均值到文本框Text2中。
注意: 不要改变函数名和参数类型。
提示:
将一系列以空格间隔的数据放入数组可以采用以下语句:
Dim A
A = Split(Trim(Text1.Text))
文本框数据以空格为分隔符把数据分割到数组A中,数组下界为0,上界为UBound(A) 展开
4个回答
展开全部
我这里有输入错误的处理功能。除了指定的意外,均采用汉字字符,增强可阅读性
Private Sub Command1_Click()
Dim 输入, 平均数 As Double
On Error GoTo 出错
If Text1 = "" Then
MsgBox "你什么都没有输入,平均谁呢?"
Exit Sub
End If
输入 = Split(Trim(Text1))
If AverageA(输入, 平均数) Then
Text2 = 平均数
Else
GoTo 出错
End If
Exit Sub
出错:
MsgBox "你的输入有问题呢"
Text1 = ""
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
Command1.Caption = "平均"
End Sub
Private Function AverageA(数组 As Variant, 结果 As Double) As Boolean
Dim 循环变量 As Integer, 元素个数 As Integer
AverageA = True
On Error GoTo 出错
元素个数 = UBound(数组) + 1
For 循环变量 = 0 To 元素个数 - 1
结果 = 结果 + 数组(循环变量) / 元素个数
Next 循环变量
Exit Function
出错:
AverageA = False
End Function
Private Sub Command1_Click()
Dim 输入, 平均数 As Double
On Error GoTo 出错
If Text1 = "" Then
MsgBox "你什么都没有输入,平均谁呢?"
Exit Sub
End If
输入 = Split(Trim(Text1))
If AverageA(输入, 平均数) Then
Text2 = 平均数
Else
GoTo 出错
End If
Exit Sub
出错:
MsgBox "你的输入有问题呢"
Text1 = ""
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
Command1.Caption = "平均"
End Sub
Private Function AverageA(数组 As Variant, 结果 As Double) As Boolean
Dim 循环变量 As Integer, 元素个数 As Integer
AverageA = True
On Error GoTo 出错
元素个数 = UBound(数组) + 1
For 循环变量 = 0 To 元素个数 - 1
结果 = 结果 + 数组(循环变量) / 元素个数
Next 循环变量
Exit Function
出错:
AverageA = False
End Function
展开全部
Private Sub Command1_Click()
Dim A
A = Split(Trim(Text1.Text))
If Trim(Text1) <> "" Then
Text2.Text = AverageA(A)
Else
MsgBox "请输入数据(以空格间隔)!", vbOKOnly, "错误"
Text1.SetFocus
End If
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'只能输入数字
KeyAscii = IIf(InStr(xStr & Chr(8), Chr(KeyAscii)), KeyAscii, 0)
End Sub
Private Function AverageA(A As Variant) As Double
Dim s As Double
For i = 0 To UBound(A)
s = s + Val(A(i))
Next i
End Function
Dim A
A = Split(Trim(Text1.Text))
If Trim(Text1) <> "" Then
Text2.Text = AverageA(A)
Else
MsgBox "请输入数据(以空格间隔)!", vbOKOnly, "错误"
Text1.SetFocus
End If
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'只能输入数字
KeyAscii = IIf(InStr(xStr & Chr(8), Chr(KeyAscii)), KeyAscii, 0)
End Sub
Private Function AverageA(A As Variant) As Double
Dim s As Double
For i = 0 To UBound(A)
s = s + Val(A(i))
Next i
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Function AverageA(a() As String) As Double
Dim aa As Double
For i = 0 To UBound(a)
aa = aa + Val(a(i))
Next
AverageA = aa / i
End Function
Private Sub Command1_Click()
Dim a() As String
a = Split(Text1.Text)
Print AverageA(a)
End Sub
Dim aa As Double
For i = 0 To UBound(a)
aa = aa + Val(a(i))
Next
AverageA = aa / i
End Function
Private Sub Command1_Click()
Dim a() As String
a = Split(Text1.Text)
Print AverageA(a)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Function AverageA(A As Variant) As Double
Dim s As Double
s = 0
For i = 0 To UBound(A)
s = s + Val(A(i))
Next
If i <> 0 Then AverageA = s / i Else AverageA = 0
End Function
Private Sub Command1_Click()
Dim A
A = Split(Trim(Text1.Text))
Text2.Text = AverageA(A)
End Sub
Dim s As Double
s = 0
For i = 0 To UBound(A)
s = s + Val(A(i))
Next
If i <> 0 Then AverageA = s / i Else AverageA = 0
End Function
Private Sub Command1_Click()
Dim A
A = Split(Trim(Text1.Text))
Text2.Text = AverageA(A)
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询