高手指点我要编写一个VB程序,点击command1时,随机产生10个0到100的随机整数,并将其显示在text1.text
点击command2时。可以输入一个整数。并在text1.text中查找。如果找到则在text2.text显示其在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 展开
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 展开
2个回答
展开全部
你的错误在于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
不能定义为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来获取下标
你中间的排序部分是冒泡排序吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个问题已经在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
你的程序中,排序部分有问题,折半查找部分也存在问题。如下程序在你源程序的基础上,进行了修改。
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |