在 VB中,利用随机函数产生 10个 1---100 之间的随机整数,找出其中能被 5 整除的数并求其和

如题所述

Private Sub Command1_Click()
Text1.Text = ""
Dim AA(1 To 20) As Integer, SS As Integer, SDA As Integer
For I = 1 To 20
Randomize
AA(I) = Int(Rnd * 99 + 1)
Next I
For I = 1 To 19
    For J = 1 To 20 - I
        If AA(J) > AA(J + 1) Then
        SDA = AA(J)
        AA(J) = AA(J + 1)
        AA(J + 1) = SDA
        End If
    Next J
Next I
Text1.Text = Text1.Text & "生成的20个随机数:" & vbCrLf
For I = 1 To 20
If AA(I) < 10 Then
Text1.Text = Text1.Text & "0" & AA(I) & Space(3)
Else
Text1.Text = Text1.Text & AA(I) & Space(3)
End If
If I Mod 10 = 0 Then
Text1.Text = Text1.Text & vbCrLf
End If
Next I
Text1.Text = Text1.Text & vbCrLf
Text1.Text = Text1.Text & vbCrLf
Text1.Text = Text1.Text & "能够被5整除的数:" & vbCrLf
For I = 1 To 20
    If AA(I) Mod 5 = 0 Then
    Text1.Text = Text1.Text & AA(I) & Space(3)
    SS = SS + AA(I)
    End If
Next I
Text1.Text = Text1.Text & vbCrLf
Text1.Text = Text1.Text & vbCrLf
Text1.Text = Text1.Text & "能够被5整除的数的总和是:" & vbCrLf
Text1.Text = Text1.Text & SS
End Sub

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-02
dim rns as integer,sumf as integer,a as integer
for a=1 to 10
    rns=int(rnd*100+1)
    if rns mod 5 = 0 then sumf=sumf+rns
    debug.print rns
next
msgbox "the sum is" & sumf

相似回答