使用inputbox()函数将多个整数输入数组a,再使用冒泡法排序对对数组进行排序。

2025-01-01 11:20:35
推荐回答(1个)
回答1:

Private Sub Form_Click()

  Dim a(100) As Integer

  Dim n As Integer, i As Integer, j As Integer, t As Integer

  t = InputBox("请输入整数,-1表示结束:")

  n = 0

  Do While t <> -1

    n = n + 1

    a(n) = t

    t = InputBox("请输入整数,-1表示结束:")

  Loop

  For i = 1 To n - 1

    For j = 1 To n - i

      If a(j) > a(j + 1) Then

        t = a(j): a(j) = a(j + 1): a(j + 1) = t

      End If

    Next j

  Next i

  Print "共输入"; n; "个有效数字,排序结果如下:"

  For i = 1 To n

    Print a(i);

    If i Mod 10 = 0 Then Print

  Next i

End Sub