yuangw1234 发表于 2006-5-16 22:19:00

数组内项数为什么不能增加

本人有这样一个想法,写了以下程序,可是失败,简单的我都搞不定,希望有朋友可以帮忙修改,先谢过!
数组a(2)原有三个数 1、2、3,然后手动输入4个数,如果输入的数与a(2)中的任何一个数都不相等,那么这个数就增加到数组a中,如输入2、3、4、8,则数组a中的元素就成了1、2、3、4、8,如果与数组a中一个数相同则这个数将不增加到数组a中
Public Sub bz()
Dim a(2) As Double, b(2) As Double, shu As Integer
a(0) = 1: a(1) = 2: a(2) = 3
For I = 0 To 3
   b(I) = InputBox("请输入数", "", "")
    If b(I)a() Then
    shu = UBound(a) + 1
    ReDim Preserve a(0 To shu)
    a(shu) = b(I)
   End If
Next
End Sub

xinghesnak 发表于 2006-5-17 08:35:00

If b(I)a() Then 这句话有问题,b(i)是一个Double,而a()是个数组
改成这样:
Sub main()
Dim a() As Double, b(3) As Double, judge As Boolean
ReDim a(2) As Double
a(0) = 1: a(1) = 2: a(2) = 3
For i = 0 To 3
    b(i) = InputBox("请输入数", "", "")
    judge = False
    For j = 0 To UBound(a) - 1
      If b(i) = a(j) Then judge = True
    Next
    If judge = False Then ReDim Preserve a(UBound(a) + 1): a(UBound(a)) = b(i)
Next
End Sub

yuangw1234 发表于 2006-5-17 21:50:00

我分析过,有点出入,本人已改正,不过很感谢大师提拨
Sub main()
Dim a() As Double, b(2) As Double, judge As Boolean
ReDim a(2) As Double
a(0) = 1: a(1) = 2: a(2) = 3
For i = 0 To 2
    b(i) = InputBox("ÇëÊäÈëÊý", "", "")
    judge = False
    For j = 0 To UBound(a)
      If b(i) = a(j) Then judge = True
      If judge = True Then
            Exit For
      End If
    Next
    If judge = False Then
    ReDim Preserve a(UBound(a) + 1)
    a(UBound(a)) = b(i)   
    End If
Next
    MsgBox judge
    MsgBox UBound(a)
    MsgBox a(UBound(a))
End Sub
页: [1]
查看完整版本: 数组内项数为什么不能增加