16.小劉為班級的抽獎活動編寫了一個 VB 程序。單擊“抽取”按鈕 command1 時,在列表框 List1 中顯示按用戶抽取人數在學號起訖范圍內(最大學號不超過 100)隨機抽取到不重復的學號。程序運行界面如圖所示。 實現上述功能的 VB 程序如下,但加框處代碼有錯,請改正。 Dim xh(1To 100)As Integer Dim yx As Integer Private Sub Command1_Click
Dim n1,n2,n,x As Integer List1.Clear Randomize n1=Val(Text1.Text) n2=Val(Text2.Text) n=Val(Text3.Text) If n<=n2-n1Then yx=1 Do While yx<=n x='① If yx=1Then xh(yx)=x yx=2 Else If qc(x)=True Then xh(yx)=x yx=yx+1 End If End If Loop For i=1To n List1.AddItem Str(xh(i)) Next i Else MsgBox“你輸入的抽取人數不能實現,請重新輸入。“End If End Sub Public Function qc(t As Integer) As Boolean For j=1To yx If xh(j)=t Then qc=False Exit For End If Next j If Then qc=True'②End Function (1)加框處①有錯,應改為
17.在一個長度為 n 的數字序列中,如果相鄰元素差的絕對值經過升序排序后正好是 1 到 n-1,則認定該序列存在著“有趣的跳躍”。例如:數字序列 1、4、2、3 的相鄰數字差的絕對值分別為 3、2、1,排序后是 1、2、3,則說明該序列存在著“有趣的跳躍”。 小明編寫了 VB 程序用于數字序列的檢測,程序運行界面如圖所示。在文本框 Text1 中逐個輸入數字,依 次存儲到數組 a 中,并在列表框 List1 中顯示。單擊“判斷”按鈕 Command1,將原始數字序列相鄰數字差 的絕對值依次存儲到數組 b 中,并對數組 b 進行升序排序后在列表框 List2 中顯示;若輸入的數字序列存在“有趣的跳躍”,則在文本框 Text2 中輸出“yes”,否則輸出“no”。 (1)若輸入序列 3、13、24、35、47、30,單擊“判斷”按鈕后,文本框 Text2 中將顯示
。 (2)實現上述功能的 VB 程序如下,請在橫線處填入合適代碼。Dim n As Integer'存儲序列的元素個數 Dim a(1To 100)As Integer'存儲原始數字序列 Dim b(1To 100)As Integer'存儲相鄰數字的差值絕對值 Private Sub Form_Load ( ?。?br />n=0End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii=13Then n=n+1 ① List1.AddItem str(a(n)) Text1.Text=““ End If End Sub Private Sub Command1_Click ( ) Dim i As Integer,j As Integer,k As Integer,m As Integer Dim flag As Boolean m=n-1 For i=1To m b(i)=② Next i For i=2To m k=b(i) For j=i-1To 1Step-1 If k>b(j) Then Exit For b(j+1)=b(j) Next j ③=k Next i flag=True For i=1To m List2.AddItem str(b(i)) If b(i)<>i Then flag=False Next i If flag=True Then Text2.Text=“yes“Else Text2.Text=“no“End Sub