vb随机产生100个自然数,将它们从大到小的排序。并输出

发布网友 发布时间:2022-04-22 03:29

我来回答

4个回答

热心网友 时间:2023-06-25 16:02

Private Sub Form_Click()
Form1.AutoRedraw = True
Form1.Cls
Dim A(100) As Integer
Dim I As Integer, J As Integer, K As Integer
Randomize
Print "随机产生的100个数是:"
For I = 1 To 100
A(I) = Int(Rnd * 999 + 1)
Print A(I);
If I Mod 10 = 0 Then Print
Next
For I = 1 To 99
For J = I + 1 To 100
If A(I) < A(J) Then
K = A(I)
A(I) = A(J)
A(J) = K
End If
Next
Next
Print "从大到小排序后的顺序为:"
For I = 1 To 100
Print A(I);
If I Mod 10 = 0 Then Print
Next

End Sub

已经运行过。

热心网友 时间:2023-06-25 16:03

新建一个窗体,代码如下

Option Explicit

Private Sub Form_Load()
    '创建一个Label
    Dim t As Label
    Set t = Me.Controls.Add("VB.Label", "t")
    t.Visible = True
    t.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight

    '随机生成100个自然数
    Randomize
    Dim arr(99) As Long
    Dim i As Long
    For i = 0 To 99
        arr(i) = Rnd * 2000
    Next

    '升序排列
    BubbleSort arr, 1

    '整理结果
    Dim strOut As String
    For i = 0 To 99
        strOut = strOut & arr(i) & Space(4)
    Next
    
    '输出
    t = strOut
End Sub

Sub BubbleSort(MyArray() As Long, ByVal nOrder As Integer)
    Dim Index
    Dim TEMP
    Dim NextElement, gIterations

    NextElement = 0
    Do While (NextElement < UBound(MyArray))
        Index = UBound(MyArray)
        Do While (Index > NextElement)
            If nOrder = 1 Then
                If MyArray(Index) < MyArray(Index - 1) Then
                    TEMP = MyArray(Index)
                    MyArray(Index) = MyArray(Index - 1)
                    MyArray(Index - 1) = TEMP
                End If
            ElseIf nOrder = 2 Then
                If MyArray(Index) >= MyArray(Index - 1) Then
                    TEMP = MyArray(Index)
                    MyArray(Index) = MyArray(Index - 1)
                    MyArray(Index - 1) = TEMP
                End If
            End If
            Index = Index - 1
            gIterations = gIterations + 1
        Loop
        NextElement = NextElement + 1
        gIterations = gIterations + 1
    Loop

End Sub

热心网友 时间:2023-06-25 16:03

Private Sub Command1_Click()
Dim a(1 To 100) As String
Dim M As Integer
Dim i As Integer
Dim TEMP As Integer
Randomize
For M = 1 To 100
a(M) = Int(Rnd * 1000)
Next M
For i = 1 To 99
For j = i + 1 To 100
If a(i) > a(j) Then
TEMP = a(i)
a(i) = a(j)
a(j) = TEMP
End If
Next
Next
Text1.Text = a(1)
For t = 1 To 99
Text1.Text = Text1.Text & "," & a(t + 1)
Next
End Sub

text1显示结果

热心网友 时间:2023-06-25 16:04

Private
Sub
Form_Click()
Form1.AutoRedraw
=
True
Form1.Cls
Dim
A(100)
As
Integer
Dim
I
As
Integer,
J
As
Integer,
K
As
Integer
Randomize
Print
"随机产生的100个数是:"
For
I
=
1
To
100
A(I)
=
Int(Rnd
*
999
+
1)
Print
A(I);
If
I
Mod
10
=
0
Then
Print
Next
For
I
=
1
To
99
For
J
=
I
+
1
To
100
If
A(I)
<
A(J)
Then
K
=
A(I)
A(I)
=
A(J)
A(J)
=
K
End
If
Next
Next
Print
"从大到小排序后的顺序为:"
For
I
=
1
To
100
Print
A(I);
If
I
Mod
10
=
0
Then
Print
Next
End
Sub
已经运行过。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com