【加试题】字符串排序。编写VB程序,实现如下功能:程序运行时,单击命令按钮Command1 产生100个随机字符串(每个字符串长度不超过10),并显示在列表框List1中,单击命令按钮Command2对产生的100个随机字符串按权值进行降序排序,将排序结果显示在列表框List2中。每个字符串的权值定义为该字符串中ASCII码值最大的字母的ASCII码值,例如字符串“Hello”中ASCII码值最大的字母是“o”(ASCII码值为111),则字符串“Hello”的权值为111。程序运行效果如图所示。实现上述功能的VB代码如下,但加框处代码有错,请改正。
Dim a(1 To 100) As String '依次存放100 个随机字符串
Dim b(1 To 100) As Integer '依次存放每个字符串的权值
Private Sub Command1_Click() '产生100 个随机字符串
Dim i As Integer, k As Integer, t As Integer, x As Integer
Randomize
For i = 1 To 100
k = Int(Rnd * 10 + 1) : t = 0 'k 表示第i 个字符串的长度
Do While t < k '通过循环依次产生第i 个字符串a(i)的k 个字符
If Int(Rnd * 2 + 1) = 1 Then '产生一个小写字母的ASCII 码值
x = Asc("a") + Int(Rnd * 26)
Else
x = Asc("A") + Int(Rnd * 26) '产生一个大写字母的ASCII 码值
End If
'①
If b(i) < x Then b(i) = x
t = t + 1
Loop
List1.AddItem a(i)
Next i
End Sub
Private Sub Command2_Click() '对100 个字符串按字符串权值降序排序
Dim i As Integer, j As Integer, y As Integer
Dim s As String
For i = 1 To 99
k = i
For j = i + 1 To 100
'②
Next j
If k <> i Then
s = a(k): a(k) = a(i): a(i) = s
y = b(k): b(k) = b(i): b(i) = y
End If
Next i
For i = 1 To 100
List2.AddItem a(i)
Next i
End Sub
①{#blank#}1{#/blank#} ②{#blank#}2{#/blank#}