vb下标越界。急啊···

来源:百度知道 编辑:UC知道 时间:2024/04/28 22:40:48
这星期六就要考vb上机了,这是一道改错的正确答案,可是为什么我打的完全一样还有下标越界的问题。请高手帮帮我。我很急
Option Explicit

Option Base 1

Private Sub Command1_Click()
Dim s As String, ch As String, k As Integer
Dim a() As Integer, t As Integer, i As Integer
s = Text1
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch <> "<" And ch <> "#" Then
t = t * 10 + Val(ch)
Else
k = k + 1
ReDim Preserve a(k)
a(k) = t
t = 0
End If
Next i
For i = 1 To UBound(a) 在这里显示下标越界
If judge(a(i)) Then List1.AddItem a(i)
Next i
End Sub
Private Function judge(ByVal n As Integer) As Boolean
Do While n <> 1
If n Mod 3 <> 0 Then
Exit Function
End If
n = n \ 3
Loop
judge = True
End Function
在线等啊。拜托啊。。。对了就我的分全给也可以啊。我也不知道有多少分。全给还不行么。55555555555

“Dim a() As Integer, ”,数组a是空的,即不能容纳任何元素,在后来的代码中s = Text1
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch <> "<" And ch <> "#" Then
t = t * 10 + Val(ch)
Else
k = k + 1
ReDim Preserve a(k)
a(k) = t
t = 0
End If
Next i
才重新定义了a的大小。
检查你的Text1是否赋值了,不能为空。

Option Explicit

Option Base 1

Private Sub Command1_Click()
Dim s As String, ch As String, k As Integer
Dim a() As Integer, t As Integer, i As Integer
s = Text1

看看你的s的赋值,要么是文本给text1加上双引号,要么是文本框text1的值
text1.text
估计就错在这里了!

For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch <> "<" And ch <> "#" Then
t = t * 10 + Val(ch)
Else
k = k + 1
ReDim Preserve a(k)
a(k) = t
t = 0