实现上述功能的VB代码如下,但加框处代码有错,请改正。 Dim A(1 To 10) As Integer ’用于保存10个按从小到大顺序排列的整数 ’Form_Load事件过程产生10个整数,按升序保存在数组A中,并在标签Label1中显示Private Sub Form_Load() ’代码略 End Sub Private Sub Command1_Click() Dim i As Integer,j As Integer,m As Integer,k As Integer Dim x As Integer,f As Boolean ’变量f用于标记是否在数组中找到x x=Val(Text1.Text) i=1:j=10 f=False Do While(i<=j)And (f=False) m=Int((i+j)/2) If A(m)=x Then f=True If Then i=m+1 Else j=m-1’(1) Loop If f=True Then For k=m To 9 ’(2) Next k Label2.Caption=“” For k=1 To 9 Label2.Caption=Label2.Caption+Str(A(k))+“” Next k Else Label2.Caption=“该数没有找到” End If End Sub
实现上述功能的VB代码如下,但加框处代码有错,请改正。 Dim a(1 To 8) As Integer Dim n As Integer Private Sub Form_Load() ’n=8,排序前数据存储在数组a中,并在列表框List1中显示 ’代码略 End Sub Private Sub Command1_Click() Dim flag As Boolean ’flag值为True表示一遍加工中发生过交换 i=1 flag=True Do While ’(1) flag=False For j=n To i+1 Step-1 If a(j)<a(j-1) Then k=a(j):a(j)=a(j-1):a(j-1)=k flag=True End If Next j i=i+1 Loop Label3.Caption=“排序过程的加工遍数为”+’(2) For i=1 To n List2.Addltem Str(a(i)) Next i End Sub