VB题目:随机产生1-33之间的6个随机数(保证6个数字不重复)按照从大到小的顺序排列

在线等谢谢
2024-12-26 02:56:45
推荐回答(4个)
回答1:

Private Sub Command1_Click()
Dim num(6) As Integer
Dim i As Integer, j As Integer
Dim tmp As Integer

Randomize '防止每次生出随机数一样
For i = 0 To 5

num(i) = Int(Rnd * 53) + 1
Next

PrintNum "新生成的6个随机数为:", num()

For i = 0 To 5
For j = 0 To 5
If num(j) < num(j + 1) Then
tmp = num(j)
num(j) = num(j + 1)
num(j + 1) = tmp
End If
Next
Next

PrintNum "排序后的6个随机数为:", num()

End Sub
'------------------------------------------------------------------------以下用于打印数组

Public Function PrintNum(str As String, num() As Integer)
Dim tmpString As String
Dim i As Integer

For i = 0 To 5
tmpString = tmpString & " " & num(i)
Next

Print str & tmpString

End Function

回答2:

Private Sub Command1_Click()
Dim i As Integer, j As Integer, myOutput As String
Randomize (Timer)
For i = 1 To 6
j = Int(Rnd * 33) + 1
If InStr(1, myOutput, Format(j, "00") & " ") = 0 Then
myOutput = myOutput & Format(j, "00") & " "
Else
i = i - 1
End If
Next
For i = 33 To 1 Step -1
If InStr(1, myOutput, Format(i, "00") & " ") Then Print Format(i, "00") & " ";
Next
Print
End Sub

回答3:

Private Sub Command1_Click()
Dim a(32) As Integer, i, j, t
For i = 0 To 32
a(i) = i + 1
Next
Randomize
For i = 0 To 5
j = Int(Rnd * 33)
t = a(i)
a(i) = a(j)
a(j) = t
Next
For i = 0 To 5
For j = 0 To i
If a(i) < a(j) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next
Next
For i = 0 To 5
Print a(i);
Next
Print
End Sub

回答4:

dim vl(5) as long
'make number
vl(0)=int(1+rnd*32)
for i=1 to 5
do

flag=true
vt=int(1+rnd*32)

for j=0 to i-1
if vt=vl(j) then
flag=false
exit for
end if
next j
loop until flag
next i
'sort
for i=0 to 4
for j=i+1 to 5
if vl(j)>vl(i) then
vt=vl(i)
vl(i)=vl(j)
vl(j)=vt
end if
next j
next i