【加试题】郑强收集了某月部分品牌电脑在某城市的销售量,并设计了一个VB排序程序。程序运行界面如图所示,左边列表框List1中显示排序前数据,单击“排序”按钮Command1,在右边列表框List2中显示按销售量从低到高排序后的结果,在标签Label3中显示比较次数。程序中数组a和数组b分别存储品牌名称和对应的销售量。
Const n As Integer = 10 'n为品牌数
Dim a(1 To n) As String '数组a用于存储品牌名称
Dim b(1 To n) As Integer '数组b用于存储销售量
Private Sub Form_Load()
'此过程用于对数组a和数组b进行赋初值,并显示在List1中,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, x As Single, y As String
Dim n As Integer
n = 1
For i = 1 To 9
k = i
For j = i+1 To 10
If Then k = j
n=n+1
Next j
If ① Then
x = a(i): a(i) = a(k): a(k) = x
y = b(i): b(i) = b(k): b(k) = y
End If
Next i
For i = 1 To 10
List2.AddItem Str(a(i)) + " " + b(i)
Next i
Label3.Caption = "比较次数为:" + + "次"
End Sub