在程序设计课上,小明设计了一个程序用来计算离散型随机变量的数学期望,Text1中交替输入随机变量和相应概率,并以逗号“,”为分隔符,具体界面如下图所示。
Private Sub Cmd_Click()
Dim s As String, t As String, c As String,X As Single, p As Single, EX As Single
Dim n As Integer, k As Integer
s = Text1.Text
n = Len(s)
k = 0: EX = 0: t = ""
For i = 1 To n
c = Mid(s, i, 1)
If c = "," Then
k = k + 1
If Then
X = Val(t)
Else
p = Val(t)
EX =
End If
Else
t = t + c
End If
Next i
Text2.Text =
End Sub