16.如果兩個(gè)質(zhì)數(shù)的差為2,則這兩個(gè)質(zhì)數(shù)稱為孿生質(zhì)數(shù)對(duì);編寫VB程序,實(shí)現(xiàn)如下功能:?jiǎn)螕簟帮@示孿生質(zhì)數(shù)對(duì)”按鈕Command1,在列表框list1中顯示100以內(nèi)的所有孿生質(zhì)數(shù)對(duì)(運(yùn)行效果如圖所示)。 實(shí)現(xiàn)上述功能的VB代碼如下,但加框處代碼有錯(cuò),請(qǐng)改正。 Dim a(1To 50)As Integer Private Sub Command1_Click ( ) Dim i As Integer k=0:i=3 Do While i<=100 If Then‘(1) k=k+1 a(k)=i End If i=i+2 Loop For i=2To k If Then‘(2) List1.AddItem Str(a(i-1))+“和“+Str(a(i)) End If Next i End Sub Function prime(x As Integer) As Boolean prime=False For i=2To Int(Sqr(x)) If x Mod i=0Then prime=True Exit For End If Next i End Function。
Dim a(1To 6)As Integer Dim b(1To 6)As Integer Private Sub Command1_Click ( ?。?對(duì)數(shù)組a中的數(shù)據(jù)進(jìn)行排序 Dim i As Integer,j As Integer,k as integer a(1)=53:a(2)=18:a(3)=62:a(4)=22:a(5)=6:a(6)=25 s=““ For i=1To 5 k=i For j=i+1To 6 If a(k)>a(j) Then
Next j If i<>k Then t=a(k):a(k)=a(i):a(i)=t End If Next i For i=1 To 6 s=s+Str(a(i)) Next i Label1.Caption=s End Sub Private Sub Command2_Click ( ?。?br />b(1)=3:b(2)=8:b(3)=15:b(4)=27:b(5)=38:b(6)=49 '此處部分代碼省略 End Sub Private Sub Command3_Click ( ?。?br />'將數(shù)組a和b中的數(shù)據(jù)逐個(gè)比較后添加到新的字符串s中,并將s在label3中顯示 i=1:j=1:s=““ Do While i<=6
j<=6'如果兩數(shù)組都還有數(shù)據(jù)未合并 If
Then s=s+Str(a(i)):i=i+1 Else s=s+Str(b(j)):j=j+1 End If Loop Do While i<=6'只有數(shù)組a還有數(shù)據(jù)未合并 s=s+Str(a(i)):i=i+1 Loop Do While j<=6'只有數(shù)組b還有數(shù)據(jù)未合并 s=s+Str(b(j)):j=j+1 Loop Label3.Caption=s End Sub.