vb对随机产生10个数进行排序
OptionBase1DimiAsIntegerDimjAsIntegerDima(10)AsIntegerPrivateSubCommand1_Click()a(i)=...
Option Base 1
Dim i As Integer
Dim j As Integer
Dim a(10) As Integer
Private Sub Command1_Click()
a(i) = Int(Rnd(1) * 100 + 1)
Text1.Text = ""
Text1.txet = a(i)
For i = 1 To 9
For j = 1 To 9 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j) = t
Next j
Next i
For i = 1 To 10
Text2.Text = ""
Text2.Text = a(i)
End Sub
vb渣 新手,请问这个程序那里不对 展开
Dim i As Integer
Dim j As Integer
Dim a(10) As Integer
Private Sub Command1_Click()
a(i) = Int(Rnd(1) * 100 + 1)
Text1.Text = ""
Text1.txet = a(i)
For i = 1 To 9
For j = 1 To 9 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j) = t
Next j
Next i
For i = 1 To 10
Text2.Text = ""
Text2.Text = a(i)
End Sub
vb渣 新手,请问这个程序那里不对 展开
2个回答
展开全部
Option Base 1
Dim i As Integer
Dim j As Integer
Dim a(10) As Integer
Private Sub Command1_Click()
'1、-----------------------------------
'a(i) = Int(Rnd(1) * 100 + 1) '这里不对因为你使用了Option Base 1语句所以a()数组的最小下标是1,你这里i并没有赋值i定义类型为integer所以默认值为0,所以会显示错误。
'根据你的题目要求要产生10个随机函数,那么应该要使用循环过程,正确的写法应该是:
Randomize '建议使用该函数使程序每次运行时保证随机数都不一样
for i=1 to 10
a(i) = Int(100*Rnd) + 1 '建议随机数这样子写
next
'2、----------------------------------------------------
'Text1.Text = "" ’
'Text1.txet = a(i) 不明白你这两句是什么意思,如果是要在text1中显示所有随机数应该这么写。
text1.text=""
for i=1 to 10
text1.text=text1.text & a(i) & " " '随机数之间用空格隔开
next
'3-----------------------------------------
For i = 1 To 9
For j = 1 To 9 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j) = t
end if
Next j
Next i
'上面3进行修改如下
For i = 1 To 9
For j = i+1 To 10
If a(i) > a(j ) Then
t = a(i)
a(i) = a(j )
a(j) = t
Next j
Next i
'4-------------------------
For i = 1 To 10
Text2.Text = ""
Text2.Text = a(i)
End Sub
'上面4进行修改如下
Text2.Text = ""
For i = 1 To 10
Text2.Text =text2.text & a(i) & " "
next
End Sub
'修改后代码如下:
Option Base 1
Dim i As Integer
Dim j As Integer
Dim a(10) As Integer
Private Sub Command1_Click()
Randomize
for i=1 to 10
a(i) = Int(100*Rnd) + 1
next
text1.text=""
for i=1 to 10
text1.text=text1.text & a(i) & " "
next
For i = 1 To 9
For j = i+1 To 10
If a(i) > a(j ) Then
t = a(i)
a(i) = a(j )
a(j) = t
end if
Next
Next
Text2.Text = ""
For i = 1 To 10
Text2.Text =text2.text & a(i) & " "
next
End Sub
展开全部
参考以下代码:
Dim arr(0 To 9) As Integer
Private Sub Form_Load()
Dim i, j, k
Randomize
For i = 0 To 9
arr(i) = Int(Rnd * 100) '产生10个随机数并保持到数组arr()
Debug.Print arr(i); '输出随机数到Debug窗口
Next i
Debug.Print
For i = 0 To 9 '对10个数进行选择法排序
k = i
For j = i + 1 To 9
If arr(k) > arr(j) Then k = j
Next j
If k <> i Then
t = arr(k): arr(k) = arr(i): arr(i) = t
End If
Debug.Print arr(i);
Next i
Debug.Print
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询