'运行程序,连续单击按钮,观查结果Option Explicit '三个数不可重复 Private Sub Command1_Click() Dim a, b, c Do a = Int(Rnd * 12) + 1 b = Int(Rnd * 12) + 1 c = Int(Rnd * 12) + 1 If a / b = c And a <> b And b <> c And a <> c Then Debug.Print a; "÷"; b; "="; c Exit Do End If LoopEnd Sub '三个数可重复 Private Sub Command2_Click() Dim a, b, c Do a = Int(Rnd * 12) + 1 b = Int(Rnd * 12) + 1 c = Int(Rnd * 12) + 1 If a / b = c Then Debug.Print a; "÷"; b; "="; c Exit Do End If LoopEnd Sub
如果想在每次启动程序后,随机数不同,请用 Randomize timer 初始化随机数。Private Sub Form_Load() Randomize Timer End Sub
Private Sub Form_Load() Dim a(1) Dim b() Dim i As Integer, j As Integer, k As Integer, s For i = 1 To 12 For j = 1 To 12 If i Mod j = 0 Then a(0) = i: a(1) = j s = s & i & "÷" & j & "=" & i / j & vbCrLf ReDim Preserve b(k) b(k) = a End If Next Next MsgBox s End Sub
修改一下:Private Sub Command1_Click() Randomize k = UBound(b) i = Int((k * Rnd)) MsgBox b(i)(0) & "÷" & b(i)(1) & "=" & b(i)(0) / b(i)(1) End SubPrivate Sub Form_Load() Dim a(1) Dim i As Integer, j As Integer, k As Integer, s For i = 1 To 12 For j = 1 To 12 If i Mod j = 0 Then a(0) = i: a(1) = j s = s & i & "÷" & j & "=" & i / j & vbCrLf ReDim Preserve b(k) b(k) = a k = k + 1 End If Next Next MsgBox s End Sub
修改一下:Private Sub Command1_Click() Randomize k = UBound(b) i = Int((k * Rnd)) MsgBox b(i)(0) & "÷" & b(i)(1) & "=" & b(i)(0) / b(i)(1) End SubPrivate Sub Form_Load() Dim a(1) Dim i As Integer, j As Integer, k As Integer, s For i = 1 To 12 For j = 1 To 12 If i Mod j = 0 Then a(0) = i: a(1) = j s = s & i & "÷" & j & "=" & i / j & vbCrLf ReDim Preserve b(k) b(k) = a k = k + 1 End If Next Next MsgBox s End Sub
基于老张的代码修改的Public Class Form1 Private Structure a Dim n1 As Byte Dim n2 As Byte End Structure Private b() As a Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i As Integer, j As Integer, k As Integer For i = 1 To 12 For j = 1 To 12 If i Mod j = 0 Then ReDim Preserve b(k) b(k).n1 = i b(k).n2 = j k = k + 1 End If Next Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer, k As Integer Randomize(Microsoft.VisualBasic.Timer) k = UBound(b) i = Int((k * Rnd())) MsgBox(b(i).n1 & "÷" & b(i).n2 & "=" & b(i).n1 / b(i).n2) End SubEnd Class
'运行程序,连续单击按钮,观查结果Option Explicit
'三个数不可重复
Private Sub Command1_Click()
Dim a, b, c
Do
a = Int(Rnd * 12) + 1
b = Int(Rnd * 12) + 1
c = Int(Rnd * 12) + 1
If a / b = c And a <> b And b <> c And a <> c Then
Debug.Print a; "÷"; b; "="; c
Exit Do
End If
LoopEnd Sub
'三个数可重复
Private Sub Command2_Click()
Dim a, b, c
Do
a = Int(Rnd * 12) + 1
b = Int(Rnd * 12) + 1
c = Int(Rnd * 12) + 1
If a / b = c Then
Debug.Print a; "÷"; b; "="; c
Exit Do
End If
LoopEnd Sub
Randomize Timer
End Sub
Dim a(1)
Dim b()
Dim i As Integer, j As Integer, k As Integer, s
For i = 1 To 12
For j = 1 To 12
If i Mod j = 0 Then
a(0) = i: a(1) = j
s = s & i & "÷" & j & "=" & i / j & vbCrLf
ReDim Preserve b(k)
b(k) = a
End If
Next
Next
MsgBox s
End Sub
Randomize
k = UBound(b)
i = Int((k * Rnd))
MsgBox b(i)(0) & "÷" & b(i)(1) & "=" & b(i)(0) / b(i)(1)
End SubPrivate Sub Form_Load()
Dim a(1)
Dim i As Integer, j As Integer, k As Integer, s
For i = 1 To 12
For j = 1 To 12
If i Mod j = 0 Then
a(0) = i: a(1) = j
s = s & i & "÷" & j & "=" & i / j & vbCrLf
ReDim Preserve b(k)
b(k) = a
k = k + 1
End If
Next
Next
MsgBox s
End Sub
Randomize
k = UBound(b)
i = Int((k * Rnd))
MsgBox b(i)(0) & "÷" & b(i)(1) & "=" & b(i)(0) / b(i)(1)
End SubPrivate Sub Form_Load()
Dim a(1)
Dim i As Integer, j As Integer, k As Integer, s
For i = 1 To 12
For j = 1 To 12
If i Mod j = 0 Then
a(0) = i: a(1) = j
s = s & i & "÷" & j & "=" & i / j & vbCrLf
ReDim Preserve b(k)
b(k) = a
k = k + 1
End If
Next
Next
MsgBox s
End Sub
还有那个 Randomize Timer 中的Timer也有,谢谢帮忙~~~
Dim n1 As Byte
Dim n2 As Byte
End Structure Private b() As a Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 12
For j = 1 To 12
If i Mod j = 0 Then
ReDim Preserve b(k)
b(k).n1 = i
b(k).n2 = j
k = k + 1
End If
Next
Next
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer, k As Integer
Randomize(Microsoft.VisualBasic.Timer)
k = UBound(b)
i = Int((k * Rnd()))
MsgBox(b(i).n1 & "÷" & b(i).n2 & "=" & b(i).n1 / b(i).n2)
End SubEnd Class