vb随机排序,求指教 5
1-14,14个数,随机排序,点击按钮并显示在14个label上。要求是前三个label的数值必须大于4...
1-14,14个数,随机排序,点击按钮并显示在14个label上。
要求是前三个label的数值必须大于4 展开
要求是前三个label的数值必须大于4 展开
2个回答
展开全部
已经测试通过
代码如下:
Dim b As Integer, k As Integer
Dim a(13) As Integer '定义一个数组(有14位),默认上限为a(0)
For i = 0 To 13
a(i) = i + 1 '为a(i)赋值
Label1(i).Caption = ""
Next i
For i = 0 To 13
b = 0
Randomize '防止每次生出随机数一样
If i < 3 Then
b = Int(Rnd * (9 - i)) + 5 '在5-14里面随机
Else
b = Int(Rnd * (14 - i)) + 1 '在1-14里面随机
End If
k = 0
For j = 0 To 13
If a(j) <> 0 Then k = k + 1
If k = b Then
Label1(i).Caption = "" & a(j)
a(j) = 0
Exit For
End If
Next j
Next i
展开全部
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim d(14) As Integer
For i = 1 To 14
d(i) = i
Next
For i = 1 To 13
Randomize
j = Fix(Rnd * 14) + 1
d(0) = d(j)
d(j) = d(15 - i)
d(15 - i) = d(0)
Next
For i = 1 To 14
Label1(i - 1) = d(i)
Next
End Sub
追问
要求是前三个必须大于4
追答
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim d(14) As Integer
Dim s(4)
For i = 1 To 14
d(i) = i
Next
For i = 1 To 13
Randomize
j = Fix(Rnd * 14) + 1
d(0) = d(j)
d(j) = d(15 - i)
d(15 - i) = d(0)
Next
Do
For i = 1 To 3
If d(i) <= 4 Then
Randomize
j = Fix(Rnd * 11) + 3
d(0) = d(j)
d(j) = d(i)
d(i) = d(0)
End If
Next
Loop Until d(1) > 4 And d(2) > 4 And d(3) > 4
For i = 1 To 14
Label1(i - 1) = d(i)
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询