题库 高中信息

题干

(加试题)小李基于冒泡排序算法编写了一个VB程序,功能如下:在文本框Text1 中显示排序前的数据,单击“排序”按钮Command1,在文本框Text2 中显示剔除重复数据后的升序排序结果。程序运行界面如下图所示。

实现上述功能的VB程序如下,但加框处代码有错,请改正。
Const n = 10
Dim a(1 To n) As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer, t As Integer, bottom As Integer
'获取排序前数据依次存储在数组a 中,并在文本框Text1 中显示。代码略
bottom = n : i = 1
Do While i <= bottom - 1

For j = bottom To i + 1 Step -1

If a(j) < a(i) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
ElseIf a(j) = a(j - 1) Then ' 相邻两个数据相等,进行剔除处理
a(bottom)=a(j)   
bottom = bottom - 1
End If

Next j

i = i + 1

Loop
Text2.Text = " "
For i = 1 To bottom

Text2.Text = Text2.Text + Str(a(i))

Next i
End Sub
上一题 下一题 0.99难度 填空题 更新时间:2019-04-06 11:23:32

答案(点此获取答案解析)