VB 这个程序为什么下标越界
'生成若干个099之间的整数存于数组a(个数用inputbox输入),输出数组元素,'用inputbox函数输入要删除的元素,输出删除元素后的数组。(用动态数组实现)Di...
'生成若干个0 99之间的整数存于数组a(个数用inputbox输入),输出数组元素,
'用inputbox函数输入要删除的元素,输出删除元素后的数组。(用动态数组实现)
Dim a(), j As Integer, n As Long, i As Integer, s As Integer, k As Integer, x As Integer
n = Val(InputBox("in n:"))
For i = 1 To n
a(i) = Int(Rnd * 100) 【调试显示此处下标越界,想不通为什么】
Print Format(a(i), "@@@@");
Next i
s = Val(InputBox("in s:"))
For k = 1 To n
If s = a(k) Then Exit For
Next k
If k > n Then
MsgBox ("数组中没有该数")
Else
For j = k To n - 1
a(j) = a(j + 1)
Next j
n = n - 1
ReDim Preserve a(n)
Print
Print "删除后的结果:"
For x = 1 To n
Print Format(a(x), "@@@@");
Next x
End If
前面的问题加redim之后解决了,但是在最下面的redim preserve那里显示下标越界,请问为什么 展开
'用inputbox函数输入要删除的元素,输出删除元素后的数组。(用动态数组实现)
Dim a(), j As Integer, n As Long, i As Integer, s As Integer, k As Integer, x As Integer
n = Val(InputBox("in n:"))
For i = 1 To n
a(i) = Int(Rnd * 100) 【调试显示此处下标越界,想不通为什么】
Print Format(a(i), "@@@@");
Next i
s = Val(InputBox("in s:"))
For k = 1 To n
If s = a(k) Then Exit For
Next k
If k > n Then
MsgBox ("数组中没有该数")
Else
For j = k To n - 1
a(j) = a(j + 1)
Next j
n = n - 1
ReDim Preserve a(n)
Print "删除后的结果:"
For x = 1 To n
Print Format(a(x), "@@@@");
Next x
End If
前面的问题加redim之后解决了,但是在最下面的redim preserve那里显示下标越界,请问为什么 展开
展开全部
现在完全正常了:
Private Sub Command1_Click()
Dim a() As Integer, j As Integer, n As Long, i As Integer, s As Integer, k As Integer, x As Integer
n = Val(InputBox("in n:"))
ReDim a(n)
For i = 1 To n
a(i) = Int(Rnd * 100) '【调试显示此处下标越界,想不通为什么】
Print Format(a(i), "@@@@");
Next i
s = Val(InputBox("in s:"))
For k = 1 To n
If s = a(k) Then Exit For
Next k
If k > n Then
MsgBox ("数组中没有该数")
Else
For j = k To n - 1
a(j) = a(j + 1)
Next j
n = n - 1
ReDim Preserve a(n)
Print "删除后的结果:"
For x = 1 To n
Print Format(a(x), "@@@@");
Next x
End If
End Sub
追问
这样确实是可以的,请问原来的问题出在哪里呢?
追答
我不知道。原来的程序就可以的。只要前面加入redim a(n)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询