VB编程:单击command1,则随机产生10个0~100之间的数值,将之保存到一维数组中a中,同时显示在text1中。

单击command2,则弹出对话框,接受用户输入的任意一个数,并在一维数组中查找该数,若查找失败,则在text2中显示“不存在于数组中”,否则给出该数组中的位置。

dim a(9) as integer

private sub command1_click()
dim i as integer
randomize
for i=0 to 9
a(i)=int(rnd*101)
text1.text=text1.text & a(i) & ","
next i
end sub

private sub command2_click()
dim b as integer,i as integer,c as boolean
b=val(inputbox("请输入一个数字"))
c=false
for i=0 to 9
if b=a(i) then
c=true
exit for
end if
next
if c=false then text2.text="不存在于数组中" else text2.text="在数组中的位置=" & i+1
end sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-07
Private Sub command1_click()
Dim a(10) As Integer
Dim temp As Long
For i = 1 To 10
a(i) = Int(Rnd * 100 + 0.5)
Next i
Text1.Text = a(1) & Space(2) & a(2) & Space(2) & a(3) & Space(2) & a(4) & Space(2) & a(5) & Space(2) & a(6) & Space(2) & a(7) & Space(2) & a(8) & Space(2) & a(9) & Space(2) & a(10)
End Sub
Private Sub command2_click()
Dim a As Variant
Dim much As Long, temp As Long
much = Val(InputBox("请输入一个整数"))
a = Split(Text1.Text, " ")

For i = 0 To 8
For j = i + 1 To 9
If a(i) > a(j) Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next j
Next i
low = 0: Top = 9
f = 0
Do
t = Int((Top + low) / 2)
If much = a(t) Then
f = 1
Else
If much > a(t) Then
low = t + 1
Else
Top = t - 1
End If
End If
Loop While (low < Top) And (f = 0)
If f = 1 Then
Text2.Text = a(t)
Else
Text2.Text = "该数不存在于数组中"
End If
End Sub