李華編寫了實現(xiàn)上述功能的程序,程序功能如下:程序運行時,在文本框Text1中輸入n(1<=n<=10)單擊“產(chǎn)生”按鈕Command1產(chǎn)生n×n個隨機(jī)數(shù),隨機(jī)數(shù)的范圍為[0,9],隨機(jī)存儲在數(shù)組a中,并以矩陣的形式在列表框List1上顯示。單擊“排序”按鈕Command2,對矩陣以“行”為單位進(jìn)行升序排序,并在列表框List2輸出排序后的矩陣。在程序運行界面如下圖所示。
實現(xiàn)上述功能的VB程序如下,請在劃線處填入合適的代碼。
Dim n As Integer
Dim a(1 To 100) As Integer
Private Sub Command1_Click( )
Dim i As Integer, s As String
s = ""
For i = 1 To n * n
s = s + Str(a(i))
If i Mod n = 0 Then
List1.AddItem s
s = ""
End If
Next i
End Sub
Private Sub Command2_Click()
Dim i As Integer, j As Integer, t As Integer
Dim s As String
s = ""
For i = 1 To n * n
k = i
For j = i + 1 To
If a(j) < a(k) Then k = j
Next j
If k <> i Then t = a(k):a(k) = a(i):a(i) = t
If i Mod n = 0 Then
List2.AddItem s
s = ""
End If
Next i
End Sub