关于实时错误'6':溢出
在窗体上设置了3个文本框和3个命令按钮,通过单击command1,在弹出的对话框中输入学生分数并计算,通过单击command2,在3个文本框中分别显示及格人数,不及格人数和平均分。但是单击command2后,在Text3.Text = Str(total / n)
处总是显示“实时错误6:溢出”。麻烦大家帮我看看是哪里的问题呢?
事件过程如下:
Dim n As Single Dim n1 As Single Dim n2 As Single Dim total As Single Dim score As SinglePrivate Sub Command1_Click()start: score = InputBox("输入分数:") n = 0: n1 = 0: n2 = 0: total = 0 If score >= 60 And score <= 100 Then n1 = n1 + 1 total = total + score n = n + 1 ElseIf score < 60 And score > 0 Then n2 = n2 + 1 total = total + score n = n + 1 Else GoTo finish End If GoTo startfinish:End SubPrivate Sub Command2_Click() Text1.Text = Str(n1) Text2.Text = Str(n2) Text3.Text = Str(total / n)End SubPrivate Sub Command3_Click() n = 0: n1 = 0: n2 = 0: total = 0 EndEnd Sub
Dim n As LongDim n1 As LongDim n2 As LongPrivate Sub Command2_Click() Text1.Text = n1 Text2.Text = n2 Text3.Text = total/nEnd Sub
[解决办法]
Private Sub Command1_Click()start: ... n = 0: n1 = 0: n2 = 0: total = 0 '<- 每次循环(包括最后结束时)都清零了 ... GoTo finish ... GoTo startfinish:End SubPrivate Sub Command2_Click() Text3.Text = Str(total / n) '<- 这里运算 0/0 当然出错了End Sub