当前位置:首页 » VB程序题 » 正文

随机生成20个100以内的两位正整数,统计其中有多少个不相同的数。 ---- 第六章 数组 新编Visual Basic程序设计教程 陈斌 著

2. 随机生成20个100以内的两位正整数,统计其中有多少个不相同的数。

解,在窗体上建2个按钮,再建3个文本框(前2个文本框的 MultiLine 属性要设置为 True),此题VB代码如下:
Option Base 1
Dim a() As Integer
Private Sub Command1_Click()
    Dim N As Integer, I As Integer
    Text1 = ""
    Text2 = ""
    Text3 = ""
    Text1.SetFocus
    ReDim a(20)
    Randomize
    For I = 1 To 20
        a(I) = Int(90 * Rnd) + 10
        Text1 = Text1 & CStr(a(I)) + Space(2)
    Next I
End Sub

Private Sub Command2_Click()
    Dim Ub As Integer, I As Integer, J As Integer
    Dim k As Integer, N As Integer
    Text2 = "": Text3 = ""
    Ub = UBound(a)
    N = 1
    Do While N < Ub
        I = N + 1
        Do While I <= Ub
            If a(N) = a(I) Then
                For J = I To Ub - 1
                    a(J) = a(J + 1)
                Next J
                Ub = Ub - 1
                ReDim Preserve a(Ub)
            Else
                I = I + 1
            End If
        Loop
        N = N + 1
    Loop
    For I = 1 To UBound(a)
        Text2 = Text2 & CStr(a(I)) + Space(2)
    Next I
    Text3 = CStr(Ub)
End Sub

此题程序运行效果图如下:


---- 新编Visual Basic程序设计教程  答案 陈斌  著

打赏 支付宝打赏 微信打赏

来源:济亨网

本文链接:http://wb86.com/post/160.html

    << 上一篇 下一篇 >>

    湘公网安备 43011102000514号 - 湘ICP备08100508号