vb 数组下标越界
PrivateSubCommand1_Click()Text1.Text=""DimSAsLong,iAsLongDimA()DoReDimPreserveA(S)A(S...
Private Sub Command1_Click()
Text1.Text = ""
Dim S As Long, i As Long
Dim A()
Do
ReDim Preserve A(S)
A(S) = InputBox("请输入字符串,输入空串时结束", "输入")
S = S + 1
Loop Until A(S - 1) = ""
For i = 0 To S - 2
Text1.Text = Text1.Text & "第" & CStr(i + 1) & "个是:" & CStr(A(i)) & " "
Next i
Erase A
End Sub
为什么把Loop Until A(S - 1) = "" 改成Loop Until A(S) = "" 就提示下标越界? 展开
Text1.Text = ""
Dim S As Long, i As Long
Dim A()
Do
ReDim Preserve A(S)
A(S) = InputBox("请输入字符串,输入空串时结束", "输入")
S = S + 1
Loop Until A(S - 1) = ""
For i = 0 To S - 2
Text1.Text = Text1.Text & "第" & CStr(i + 1) & "个是:" & CStr(A(i)) & " "
Next i
Erase A
End Sub
为什么把Loop Until A(S - 1) = "" 改成Loop Until A(S) = "" 就提示下标越界? 展开
1个回答
展开全部
咱分析分析这么几句话。
Do
ReDim Preserve A(S)
A(S) = InputBox("请输入字符串,输入空串时结束", "输入")
S = S + 1
Loop Until A(S - 1) = ""
运行到 ReDim Preserve A(S),数组最大下标是S,假设是5。
运行到A(S) = InputBox("请输入字符串,输入空串时结束", "输入"),这句无关大碍,略。
运行到S = S + 1,注意此S非彼S了,比刚才大了1,按刚才的假设,是6.
如果按你的修改,运行到Loop Until A(S) = "" ,你想想,S大了1是6,所以A(S) 根本没有数据和他对应,A(6)根本就是空的。当然下标越界啦。
Do
ReDim Preserve A(S)
A(S) = InputBox("请输入字符串,输入空串时结束", "输入")
S = S + 1
Loop Until A(S - 1) = ""
运行到 ReDim Preserve A(S),数组最大下标是S,假设是5。
运行到A(S) = InputBox("请输入字符串,输入空串时结束", "输入"),这句无关大碍,略。
运行到S = S + 1,注意此S非彼S了,比刚才大了1,按刚才的假设,是6.
如果按你的修改,运行到Loop Until A(S) = "" ,你想想,S大了1是6,所以A(S) 根本没有数据和他对应,A(6)根本就是空的。当然下标越界啦。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询