vb编程:从一个数组中删除一个数
从数组中删除一个数。定义一个一维数组(3,15,72,39,41,56,74,81,97,15)。从键盘上输入一个数,若这个数在数组当中,就把它删除;若这个数不在数组中,...
从数组中删除一个数。定义一个一维数组(3,15,72,39,41,56,74,81,97,15)。从键盘上输入一个数,若这个数在数组当中,就把它删除;若这个数不在数组中,提示“该数不在数组中”
要求:1)如果数组中存在多个与该数相同的元素,每一个都能删除;
2)能实现多次删除
请注意“若这个数不在数组中,提示‘该数不在数组中’”的要求~
谢谢了!!!! 展开
要求:1)如果数组中存在多个与该数相同的元素,每一个都能删除;
2)能实现多次删除
请注意“若这个数不在数组中,提示‘该数不在数组中’”的要求~
谢谢了!!!! 展开
1个回答
展开全部
Dim a()
Private Sub Command1_Click()
Dim n As Integer
n = Val(InputBox("输入一个数"))
Dim i As Integer
Dim j As Integer
Dim k As Boolean
i = LBound(a)
While i <= UBound(a)
If a(i) = n Then
k = True
For j = i To UBound(a) - 1
a(j) = a(j + 1)
Next j
ReDim Preserve a(LBound(a) To UBound(a) - 1)
i = i - 1
End If
i = i + 1
Wend
If k = False Then
MsgBox "该数不在数组中"
End If
Print "当前数组列表:";
For i = LBound(a) To UBound(a)
Print a(i);
Next i
Print
End Sub
Private Sub Form_Load()
a = Array(3, 15, 72, 39, 41, 56, 74, 81, 97, 15)
End Sub
Private Sub Command1_Click()
Dim n As Integer
n = Val(InputBox("输入一个数"))
Dim i As Integer
Dim j As Integer
Dim k As Boolean
i = LBound(a)
While i <= UBound(a)
If a(i) = n Then
k = True
For j = i To UBound(a) - 1
a(j) = a(j + 1)
Next j
ReDim Preserve a(LBound(a) To UBound(a) - 1)
i = i - 1
End If
i = i + 1
Wend
If k = False Then
MsgBox "该数不在数组中"
End If
Print "当前数组列表:";
For i = LBound(a) To UBound(a)
Print a(i);
Next i
End Sub
Private Sub Form_Load()
a = Array(3, 15, 72, 39, 41, 56, 74, 81, 97, 15)
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询