首先click要清除list視窗,不然會累加,問了兩次,分不要浪費了!! Option Base 1 Dim a() As Integer, n As Integer, i As Integer Private Sub Command1_Click() List1.Clear List2.Clear n = Val(InputBox("輸入陣列個數")) ReDim a(1 To n) Randomize For i = 1 To n a(i) = Int(90 * Rnd) + 10 List1.AddItem a(i) Next i End Sub Private Sub Command2_Click() List2.Clear Dim t As Integer For i = 1 To n For j = i To n If a(i) > a(j) Then t = a(j) a(j) = a(i) a(i) = t End If Next j Next i For i = 1 To n List2.AddItem a(i) Next i End Sub Private Sub Command3_Click() Dim i%, j%, x% x = Val(InputBox("插入的數")) For i = n To 1 Step -1 If x > a(i) Then Exit For Next i ReDim Preserve a(UBound(a) + 1) For j = UBound(a) To i + 1 Step -1 a(j) = a(j - 1) Next j a(i + 1) = x List2.Clear For i = 1 To UBound(a) List2.AddItem a(i) Next i End Sub 這樣可以了吧?
首先click要清除list視窗,不然會累加,問了兩次,分不要浪費了!! Option Base 1 Dim a() As Integer, n As Integer, i As Integer Private Sub Command1_Click() List1.Clear List2.Clear n = Val(InputBox("輸入陣列個數")) ReDim a(1 To n) Randomize For i = 1 To n a(i) = Int(90 * Rnd) + 10 List1.AddItem a(i) Next i End Sub Private Sub Command2_Click() List2.Clear Dim t As Integer For i = 1 To n For j = i To n If a(i) > a(j) Then t = a(j) a(j) = a(i) a(i) = t End If Next j Next i For i = 1 To n List2.AddItem a(i) Next i End Sub Private Sub Command3_Click() Dim i%, j%, x% x = Val(InputBox("插入的數")) For i = n To 1 Step -1 If x > a(i) Then Exit For Next i ReDim Preserve a(UBound(a) + 1) For j = UBound(a) To i + 1 Step -1 a(j) = a(j - 1) Next j a(i + 1) = x List2.Clear For i = 1 To UBound(a) List2.AddItem a(i) Next i End Sub 這樣可以了吧?