Private Sub Command1_Click()
Dim i As IntegerDim a(9) As Integer
For i = 1 To 9a(i) = Int((Rnd * 9) + 1)Print a(i)Next i
End Sub
这个随机函数的范围是1到9,而我呢 想让它产生1到9 不重复的数。很简单的,只是刚学VB不适应 ,希望你们都能帮我。这个帖子今天11:00揭贴谁给 详细的原代码,给50分。
Dim i As IntegerDim a(9) As Integer
For i = 1 To 9a(i) = Int((Rnd * 9) + 1)Print a(i)Next i
End Sub
这个随机函数的范围是1到9,而我呢 想让它产生1到9 不重复的数。很简单的,只是刚学VB不适应 ,希望你们都能帮我。这个帖子今天11:00揭贴谁给 详细的原代码,给50分。
解决方案 »
- plc上位机软件编程怎么实现(用vb或c++)
- 广州聚会居然发现某人和我很像,哈哈
- vb+sqlserver,如何即時捕捉sqlserver的錯誤作出即時反應呢?
- 为什么我的Access不让添加新的记录??
- vb用printer打印,正常走纸打印没有问题。但切换走纸方向如何打印?
- [出售]VB成型网络软件源码!!!
- 向VB达人请教DCOM方法调用问题
- 国产的“易语言”
- 编译成伪代码能正常操作,编译成本机代码操作时程序无响应。
- 我安了vb6sp5怎么在保存工程和工程属性里还是乱码啊,和原来没区别啊。要怎么样才能消除那些乱码?虽然不影响使用,但看着心烦。
- 请教个问题 ------------------ 在线等待
- 怎不让Text控件弹出右键菜单
Dim temp As Integer
Dim i As Integeri = 1
While i < 9
temp = Int((Rnd * 9) + 1)
If Rep(a, temp, i) = True Then
a(i) = temp
i = i + 1
End If
Print a(i)
WendEnd Sub
Private Function Rep(b() As Integer, T As Integer, n As Integer) As Boolean
Dim i As Integer
For i = 1 To n
If b(i) = T Then
Rep = False
Exit Function
End If
Next
Rep = True
End Function
如此循环50次,1-9的九个数字的顺序即被随即打乱.
现在你只需从mysz(0)一直取到mysz(8)即可以随机顺序取得1-9间的随机数,而且不重复.
Dim use As Integer
Dim a(9) As Integer For i = 1 To 9
use = Int((Rnd(1) * 9) + 1)
For j = 0 To i - 1
If a(j) = use Then
i = i - 1
Exit For
Else
a(i) = use
End If
Next j
Next iEnd Sub
Dim temp As Integer
Dim i As Integeri = 1
While i <= 9
temp = Int((Rnd(3) * 9) + 1)
If Rep(a, temp, i) = True Then
a(i) = temp
i = i + 1
End If
WendEnd Sub
Private Function Rep(b() As Integer, T As Integer, n As Integer) As Boolean
Dim i As Integer
For i = 1 To n
If b(i) = T Then
Rep = False
Exit Function
End If
Next
Rep = True
End Function
Dim i As Integer
Dim j As Integer
Dim a() As Integer
Dim intTemp As Integer
ReDim a(0)
For i = 1 To 9
Do While True
intTemp = Int((Rnd * 9) + 1)
For j = 0 To UBound(a)
If a(j) = intTemp Then
i = i - 1
Exit Do
End If
Next
ReDim Preserve a(i - 1)
a(i - 1) = intTemp
Debug.Print a(i - 1)
Exit Do
Loop
Next i
End Sub
Dim i As Integer
For i = 0 To 8
mysz(i) = i + 1初始化
Next i
End SubPrivate Sub Command1_Click()
Dim A As Integer, B As Integer, t As Integer, num As Integer
Dim str As String
num = 0
For num = 0 To 49
Randomize Timer
A = Int(Rnd() * 9)
Randomize Timer
B = Int(Rnd() * 9)
t = mysz(A): mysz(A) = mysz(B): mysz(B) = t'交换
Next num
For t = 0 To 8
str = str & mysz(t)
Next t
Print str'打印
End Sub
Private Sub Command1_Click() If Timer1.Enabled = True Then
Timer1.Enabled = True
Else
Timer1.Enabled = True
End IfEnd Sub
Private Sub Rep()
Dim i As Integer
Dim temp As Integer
Dim temp1 As Integer
temp = Int((Rnd(3) * 9) + 1)
temp1 = a(1)
a(1) = a(temp)
a(temp) = temp1
End SubPrivate Sub Form_Load()
Dim i As Integer
For i = 1 To 9
a(i) = i
Next
End SubPrivate Sub Timer1_Timer()
Call Rep
End Sub
Dim I As Integer
Dim intArray(10) As Integer
Dim intRnd As Integer
Dim dblK As Double
Randomize
Do While True
intRnd = Val(Left(Rnd, 1))
If Not IsInArray(intRnd, intArray) Then
intArray(I) = intRnd
Debug.Print intArray(I)
I = I + 1
End If
If I = 9 Then
Exit Do
End If
Loop
End SubPrivate Function IsInArray(ByVal intItemToFind As Integer, ByRef intArray() As Integer) As Boolean
Dim I As Integer
For I = LBound(intArray) To UBound(intArray())
If intArray(I) = intItemToFind Then
IsInArray = True
Exit Function
End If
Next
IsInArray = False
End Function