• 主页

# 《今天11点揭贴。希望11点前能够出现救世主》这 几天接触到VB。做了个简单的游戏，现在碰到个小麻烦。关于随机函数的问题。希望大家能帮

baizhongri 2003-12-18 09:12:59
Private Sub Command1_Click()
Dim i As Integer

Dim a(9) As Integer
For i = 1 To 9

a(i) = Int((Rnd * 9) + 1)

Print a(i)

Next i
End Sub

...全文
26 点赞 收藏 12

12 条回复

SmileSkyNet 2003-12-18

lgs666 2003-12-18
Option Explicit

Private Sub Command1_Click()
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 Sub

Private 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

landongfang 2003-12-18
Private a(1 To 9) As Integer
Private Sub Command1_Click()

If Timer1.Enabled = True Then
Timer1.Enabled = True
Else
Timer1.Enabled = True
End If

End 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 Sub

Dim i As Integer
For i = 1 To 9
a(i) = i
Next
End Sub

Private Sub Timer1_Timer()
Call Rep
End Sub

hcj2002 2003-12-18
0--9不重复的数？？？

liyan010 2003-12-18

Dim mysz(8) As Integer'9维数组

Dim i As Integer
For i = 0 To 8
mysz(i) = i + 1初始化
Next i
End Sub

Private 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

SoHo_Andy 2003-12-18

Private Sub Command1_Click()
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

landongfang 2003-12-18
Private Sub Command1_Click()

Dim a(1 To 9) As Integer
Dim temp As Integer
Dim i As Integer

i = 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
Wend

End 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

ides 2003-12-18

jhzhou882 2003-12-18
Private Sub Command1_Click()

Dim i, j As Integer
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 i

End Sub

liyan010 2003-12-18

landongfang 2003-12-18
Private Sub Command1_Click()

Dim a(1 To 9) As Integer
Dim temp As Integer
Dim i As Integer

i = 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)
Wend

End 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

9个不重复的数？范围是多少

VB基础类

7451

VB 基础类