VB实时错误6 溢出

来源:百度知道 编辑:UC知道 时间:2024/06/09 03:17:38
代码:
Dim tim(20)
Dim typ(20)
Dim x As Integer
Dim a As Integer
Dim t As Integer

Private Sub Command1_Click()
If Label2.Caption = Text1.Text Then
a = a + 1
x = Rnd * 20
Randomize
Label2.Caption = typ(x)
t = Int(tim(x))
Label1.Caption = "还剩" & t & "秒"
Label4.Caption = "已完成" & a & "个"
Else
Print "输入错误,时间减3秒"
t = t - 3
End If
If t < 0 Then
MsgBox "你失败了"
a = 0
Label4.Caption = "已完成" & a & "个"
End If
End Sub

Private Sub Form_Load()
Open App.Path & "\练习文字.txt" For Input As #1
For i = 0 To 20
Line Input #1, typ(i)
Next
For ii = 0 To 20
Line Input #1, tim(ii)
Next
Close #1
x = Rnd * 20
Randomize
Debug.Print x
Label2.Caption = typ(x)
t = Int(tim(x))
Label1.Caption = "还剩" & t & "秒&quo

程序没有出错
由于你是固定读取20行内容,然后又循环20次读取,这样就等于读取40行内容,问题可能是因为你的文本文件中没有40行的内容,所以会溢出吧。
解决方法,在你的那个文本文件中放入足够多的行数的数据。
我已经按照你的意思自己编了这样一段代码看了一下,测试成功的。

Dim tim(20) As String
Dim typ(20) As String
Dim x As Integer
Dim a As Integer
Dim t As Integer

Private Sub Command1_Click()
If Label2.Caption = Text1.Text Then
a = a + 1
x = Rnd * 20
Randomize
Label2.Caption = typ(x)
t = Int(tim(x))
Label1.Caption = "还剩" & t & "秒"
Label4.Caption = "已完成" & a & "个"
Else
Print "输入错误,时间减3秒"
t = t - 3
End If
If t < 0 Then
MsgBox "你失败了"
a = 0
Label4.Caption = "已完成" & a & "个"
End If
End Sub

Private Sub Form_Load()
Dim i As Integer, ii As Integer
Open App.Path & "\练习文字.txt" For Input As #1
For i = 0 To 20
Line Input #1, typ(i)
Next
Close #1
Op