高手指点我要编写一个VB程序,点击command1时,随机产生10个0到100的随机整数,并将其显示在text1.text

点击command2时。可以输入一个整数。并在text1.text中查找。如果找到则在text2.text显示其在text1.text中的位置 ,如果没找到,则显示“该数不存在于数组中”。下面这个程序有什么错吗?为什么a=split(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("请输入一个整数"))
For i = 1 To 8
For j = i + 1 To 9
a = Split(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
flag = False
Do While flage = False And low <= high
t = Int((Top + low) / 2)
If much = a(t) Then
flage = True
ElseIf a(t) > much Then
Top = t - 1
Else
low = t + 1
End If
Loop
If flage = True Then
Text1.Text = "该数在第" & t & "个位置"
Else
Text2.Text = "该数不存在于数组中"
End If

第1个回答  2011-05-08
你的错误在于command2中a() 的定义
不能定义为a()数组类型的Variant,如果这样用,你的数组维数没有定义;
改正代码:(你的command2中的代码是对a进行排序之后,查找,违背了你提问中的描述!我是按照你提问中的方式修改的,重新定义b as variant类型)
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 b As Variant
Dim i As Long
Dim much As Long, temp As Long

much = Val(InputBox("请输入一个整数"))

For i = 1 To 8
For j = i + 1 To 9
a = Split(Text1.Text, " ")

If a(i) > a(j) Then
temp = a(i)
a(j) = temp
a(i) = a(j)
End If

Next j
Next i

Dim flage As Boolean
flage = False
b = Split(Text1.Text, " ")
For i = LBound(b) To UBound(b)
If b(i) = much Then
flage = True
Exit For
End If
Next
If flage = True Then
Text2.Text = "该数在第" & i + 1 & "个位置"
Else
Text2.Text = "该数不存在于数组中"
End If

End Sub追问

split函数产生的一维数组下标是从0开始的, For i = 1 To 8
For j = i + 1 To 9
a = Split(Text1.Text, " ")这段排序程序不是没将0这个位置的数参与排序吗?

追答

Option base 1 是可以定义数组的下标起始值,但是对于Array定义的数组和Split赋予的vartiant类型数组无效
你可以使用Lbound 和Ubound来获取下标
你中间的排序部分是冒泡排序吧

追问

但是排序那儿应该从0开始啊

追答

那你的代码中Option base 1就不需要了
还有你要知道a(10)那表示下标从0-10, 11个数值啊!你可以这样定义 a(0 to 9)

追问

能在百度hI里加我为好友吗?split返回的函数下标是从零开始的,排序也应该是从零开始,即for i=0 to 8,j=i+1 to 9这样十个数没错啊。如果是for i=1 to 8,那么0这个位置的数不没参与排序吗?望指点。请同意加我为好友。以后还望多多指教,不胜感激、

追答

你加我吧,这样会,你的财富值消耗的厉害啊
!!

第2个回答  推荐于2017-10-02
这个问题已经在http://zhidao.baidu.com/question/262052332.html给予了回答。

你的程序中,排序部分有问题,折半查找部分也存在问题。如下程序在你源程序的基础上,进行了修改。
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 = "该数在第" & t + 1 & "个位置" '声明:此时的位置是排序后的位置,不是原始位置
Else
Text2.Text = "该数不存在于数组中"
End If
End Sub本回答被提问者采纳

高手指点我要编写一个VB程序,点击command1时,随机产生10个0到100的...
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) ...

用VB编程:随机产生10个0~100之间的随机整数,存放在一个数组中,然后求个...
Private Sub Form_Load()Dim a(10), i, x, j As Integer Dim y As String For i = 0 To 9 Randomize a(i) = Int(Rnd(1) * 101)x = x + a(i)Next i Print "元素和=" & x Print "平均值=" & x \/ 10 For i = 0 To 9 If a(i) > (x \/ 10) Then j = j + ...

VB编程题目: 随机产生10个0-100之间的整数放入数组,显示数组各元素的值...
Private Sub Command1_Click()Dim a(9)Randomize Dim sum As Integer, min As Integer, max As Integer sum = 0: min = 100: max = 0 For i = 0 To 9 a(i) = Int(101 * Rnd))sum = sum + a(i)If a(i) < min Then min = a(i)If a(i) > max Then max = a(i)Pr...

计算机VB中,利用随机函数产生10个10-100的随机数,并将其赋值给数组a...
Private Sub Command1_Click() Dim a(10) As Integer Randomize For i = 1 To 10 a(i) = Int(Rnd() * 91) + 10 Print a(i); s = s + a(i) Next i Print Print "平均值="; s \/ 10End Sub

VB中,随机产生10个0~100之间的随机数,按从小到大的顺序排列怎么做...
1 - 100 之间选 20 个随机数Trec = 10 End Sub Private Sub Command1_Click() S = GetRnd(1, 100, Trec) Me.ClsFor i = 0 To Trec - 1 Print S(i) Next i End Sub Private Sub Command2_Click()'*** 由小排到大(冒泡排序法)Me.Cls For i = 0 To Trec - 1 k = i For...

vb编写程序,利用随机函数产生并显示10 个1~100 之间的随机整数并找出这...
Private Sub Command1_Click() Dim a(10) As Integer Randomize Max = 1 For i = 1 To 10 a(i) = 1 + Int(Rnd * 100) Print a(i); If a(i) > a(Max) Then Max = i Next i Print Print "a("; Max; ")="; a(Max)End Sub ...

...VB程序设计) 要求:⑴ 随机产生10个1-100的整数,并对这些数用冒泡排序...
Private Sub Command1_Click()Cls ReDim a(9)Randomize For i = 0 To 9 a(i) = Int(Rnd * 900) + 100 Next Print Join(a);End Sub Private Sub Command2_Click()For i = 0 To 8 For j = 8 To i + 1 Step -1 If a(j) < a(j - 1) Then t = a(j)a(j) = a(j...

vb编程:随机产生10个1–100之间的整数,并按从小到大顺序输出,谁能帮我...
Private Sub Command1_Click()Dim a(10), i, j As Integer For i = 0 To 9 a(i) = Int(Rnd * 100) + 1 Print a(i);Next For i = 0 To 8 For j = i To 9 If a(i) > a(j) Then t = a(i)a(i) = a(j)a(j) = t End If Next Next Print For i = 0 To...

...从小到大排列。打印在窗体。请各位用VB程序代码写出来。谢谢_百度知 ...
添加一个命令按钮,复制一下代码: Option Explicit Private Sub Command1_Click() Cls Randomize Dim i(9) As Integer, j As Integer, k As Integer, max As Integer, pos As Integer, tmp As Integer max = 0: pos = -1 Print "===原始数据" For j = 0 To 9 '生成随机...

利用VB制作一个“随机生成十个数字(1-100之间的整数)求极值(最大最小...
代码:Dim max, min As IntegerDim avg As DoublePrivate Sub Command1_Click()Dim num(10) As IntegerText1.Text = ""Label4.Caption = ""Label5.Caption = ""Label6.Caption = ""Dim i, j, temp, sum As Integersum = 0For i = 1 To 10num(i) = Int(Rnd * (100 + 1))sum...

相似回答