我要编写一个程序,单击command1时,在text1中产生十个0到100的随机整数,并将其保存在一维数组中。单击co

输入任意数,单击command2时,在text2中显示其位置,如没有该数,则提示该数不存在。请问下面这段程序有什么错吗?怎么会提示下表越界。有时候我改了之后却发现出现随机数的重复率高得可怕。
Option Base 1
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()

Dim much As Long, temp As Long
much = Val(InputBox("请输入一个整数"))
For i = 1 To 8
For j = i + 1 To 9
a = Array(Text1.Text)
If a(i) > a(j) Then
temp = a(i)
a(j) = temp
a(i) = a(j)
End If
Next j
Next i
low = 1: Top = 10: t = Int((Top + low) / 2)
For i = 1 To 10
If much = a(t) Then
Text2.Text = a(t)
ElseIf much > a(t) Then
low = t + 1
ElseIf much < a(t) Then
Top = t - 1
Else
Text2.Text = "该数不存在于数组中"
End If
Next i
End Sub

June 17 , I seem to settle in the boiling last night , let me what a night of excess hormones .
eagle from the Pampas plateau were to Cuikulaxiu trend , will pull into a 0-6 Serbia and Montenegro into humiliation . I can not give your eyes back in the history of the long gallery,Wholesale sunglasses, to find a similar scene , perhaps somewhere in the fate of the arrangements.
1978 , the era of football into Argentina that year, we could actually pleasantly surprised to find a very similar scene. Argentines have a rematch in the final game to defeat more than four goals in the group stage draw with the Dutch team on Peru,Discount Mulberry, before being final. At the time,mulberry handbags, it is almost impossible to be regarded as a miracle , then , the eagle who was with the Pampas like crazy Peruvians destroyed the offensive line , this gave its all -powerful Dutch defense could do nothing , and ultimately Let Argentines ripped through the six ! Argentina boiling , and they with the perfect result of a showdown with the Dutch to win the chance !
6-0, actually so similar history . Must not lose points in a game , another Argentine -style with a 6-0 win over the fate and usher in a decisive battle with the Dutch . Although 78 years is different from the match with the Netherlands only a group stage only , but we must keep in mind that this unusual process similar to a large somewhere Could it portends ?
year, Kempes flowing hair into the world of eternal memories, year after ,6 -0 , Argentina began a new era , and today is the same score , different experiences, will face Another is the same opponent, Pampas eagle who , whether to display the same perfect wings of glory ?
I look forward to , Argentina + perfect attack = winner!
massacre continues ... ...
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-05-05
问题在于a = Array(Text1.Text)语句并没有将Text1.Text中的值转换成数组追问

那么该怎么 办呢

追答

你的程序中,排序部分有问题,折半查找部分也存在问题。如下程序在你源程序的基础上,进行了修改。
Option Base 1
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

本回答被提问者采纳
相似回答