vb6.0 array数组求大神指出错误
2、利用Array函数给数组a赋值(数组元素的个数及每个元素的值自己设定),从键盘上输入一个数据,在数组中进行查找,如果数组中有,则删除数组中的这个元素,如果没有则提示“...
2、利用Array函数给数组a赋值(数组元素的个数及每个元素的值自己设定),从键盘上输入一个数据,在数组中进行查找,如果数组中有,则删除数组中的这个元素,如果没有则提示“没有这个数”。
【提示】删除数组中的某个元素就是将这个元素之后的元素分别往前移动一个位置,如数组原来是11,22,33,44,55,66,77,88共8个元素,如果要删除值为44的元素,其实就是将后面的55,66,77,88往前移一位,覆盖原来的数据,然后再重新声明数组有7个元素即可。
编码:Private Sub Command1_Click()
Dim x(), i, j, p, u, n As Integer
p = 0
x = Array(12, 78, 45, 65, 32, 12, 11, 22, 88, 94)
n = UBound(x)
Print "原数组"
For i = LBound(x) To UBound(x)
Print x(i);
Next i
Dim a As Integer
a = InputBox("输入一个数")
For i = LBound(x) To UBound(x)
If x(i) = a Then
For j = i To UBound(x) - 1
x(j) = x(j + 1)
Next j
ReDim Preserve x(n - 1)
Else: p = p + 1
End If
Next i
If p = 10 Then
MsgBox ("没有这个数")
End If
End Sub
Private Sub Command2_Click()
For u = LBound(x) To UBound(x)
Print x(u);
Next u
End Sub
实时错误‘9’
下标超出
求各路大神,指点小白 展开
【提示】删除数组中的某个元素就是将这个元素之后的元素分别往前移动一个位置,如数组原来是11,22,33,44,55,66,77,88共8个元素,如果要删除值为44的元素,其实就是将后面的55,66,77,88往前移一位,覆盖原来的数据,然后再重新声明数组有7个元素即可。
编码:Private Sub Command1_Click()
Dim x(), i, j, p, u, n As Integer
p = 0
x = Array(12, 78, 45, 65, 32, 12, 11, 22, 88, 94)
n = UBound(x)
Print "原数组"
For i = LBound(x) To UBound(x)
Print x(i);
Next i
Dim a As Integer
a = InputBox("输入一个数")
For i = LBound(x) To UBound(x)
If x(i) = a Then
For j = i To UBound(x) - 1
x(j) = x(j + 1)
Next j
ReDim Preserve x(n - 1)
Else: p = p + 1
End If
Next i
If p = 10 Then
MsgBox ("没有这个数")
End If
End Sub
Private Sub Command2_Click()
For u = LBound(x) To UBound(x)
Print x(u);
Next u
End Sub
实时错误‘9’
下标超出
求各路大神,指点小白 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询