明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1361|回复: 2

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

[复制链接]
发表于 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

发表于 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

 楼主| 发表于 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

 

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-27 04:23 , Processed in 0.155042 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表