注册 登录
编程论坛 VB6论坛

我真是小白,哪位大佬出手相助

YMH030109 发布于 2020-03-23 18:21, 2120 次点击
随机产生10个50以内的正整数,从大到小排序,并显示排序前和排序后的结果。
3 回复
#2
YMH0301092020-03-23 20:57
随机产生10个50以内的正整数,从大到小排序,并显示排序前和排序后的结果
#3
HVB62020-03-24 08:31

Private Sub test()
   Dim i As Integer
    Dim j As Integer
    Dim a(10) As Integer
    Dim s, s1
    For i = 1 To 10
        a(i) = Round(Rnd * 50, 0) + 1
        For j = 1 To i - 1
        If a(i) = a(j) Then
              i = i - 1
        
        End If
        Next j
       '  Range("a" & i) = a(i) '去掉此行的第一个注释符号,此代码可在Excl的代码区运行。
        s = s & "," & a(i)
    Next i
      MsgBox s  '显示数组
        M = 1
For i = 1 To 9
If a(i) >= a(i + 1) Then
  If i > M Then
   M = i
  Else
   i = M
  End If
  GoTo kk:
Else
x = a(i)
a(i) = a(i + 1)
a(i + 1) = x
If i <> 1 Then i = i - 2
End If
kk:
Next i
  For i = 1 To 10
    ' Range("b" & i) = a(i)   '去掉此行的第一个注释符号,此代码可在Excl的代码区运行。
    s1 = s1 & "," & a(i)
Next i
     MsgBox s1  '显示数组
End Sub
#4
ZHRXJR2020-03-27 17:01
只有本站会员才能查看附件,请 登录

程序代码:

Dim AA(1 To 10) As Integer, BB(1 To 10) As Integer

Private Sub Command1_Click()
For I = 1 To 10
    BB(I) = AA(I)
Next I
Dim KK As Integer
For I = 1 To 9
    For J = 1 To 10 - I
        If BB(J) > BB(J + 1) Then
            KK = BB(J)
            BB(J) = BB(J + 1)
            BB(J + 1) = KK
        End If
    Next J
Next I
Text2.Text = ""
For I = 1 To 10
    If BB(I) < 10 Then
        Text2.Text = Text2.Text & "0" & CStr(BB(I)) & Space(5)
    Else
        Text2.Text = Text2.Text & CStr(BB(I)) & Space(5)
    End If
Next I
End Sub

Private Sub Command2_Click()
Unload Me
Form1.Show
End Sub

Private Sub Form_Load()
For I = 1 To 10
    Randomize
    AA(I) = Int(Rnd * 50 + 1)
Next I
Text1.Text = ""
For I = 1 To 10
    If AA(I) < 10 Then
        Text1.Text = Text1.Text & "0" & CStr(AA(I)) & Space(5)
    Else
        Text1.Text = Text1.Text & CStr(AA(I)) & Space(5)
    End If
Next I
End Sub
1