主题:[讨论]如何随机产生1-5
网络毛毛虫
[专家分:0] 发布于 2008-10-03 15:58:00
小弟 想请教大家一个问题。
请看图,图中红圈的 数字,需要随机从1-5中产生,也就是每次新游戏1-5数字的位置都要不同。
白色的框 和绿色的框都是 按钮,游戏要求 拖拽 白色按钮到 绿色按钮,并绿色按钮上显示 白色按钮的数字。
小弟 两个问题:
1.如何才能让1-5随机在白色按钮上产生。
2.如何实现上述拖拽。
谢谢大家了,新手学VB,实在是很弱啊 谢谢啦。
[img]http://i37.tinypic.com/2058p5f.jpg[/img]
回复列表 (共3个回复)
沙发
网络毛毛虫 [专家分:0] 发布于 2008-10-03 16:26:00
第一个问题 解决啦
用了个比较笨的办法 哈哈
Dim myval1 As Integer
Dim myval2 As Integer
Dim myval3 As Integer
Dim myval4 As Integer
Dim myval5 As Integer
myval1 = Int((5 * Rnd()) + 1)
While myval2 = myval1
myval2 = Int((5 * Rnd()) + 1)
End While
While myval3 = myval2 Or myval3 = myval1
myval3 = Int((5 * Rnd()) + 1)
End While
While myval4 = myval3 Or myval4 = myval2 Or myval4 = myval1
myval4 = Int((5 * Rnd()) + 1)
End While
While myval5 = myval4 Or myval5 = myval3 Or myval5 = myval2 Or myval5 = myval1
myval5 = Int((5 * Rnd()) + 1)
End While
板凳
我是大喊三 [专家分:3010] 发布于 2008-10-03 17:23:00
楼上的是为了不重复吗?
3 楼
一江秋水 [专家分:9680] 发布于 2008-10-04 09:29:00
要完成你的要求,最好设置两个按纽控件数组,你所说的白色按纽为Command1,其index属性为1-5,它们的DragMode属性均须设置为1;你所说的绿色按纽为Command2,其index属性也为1—5,另外再添加一个产生随机数的按纽Command3。代码如下:
Option Explicit
Dim NewV As String
Private Sub Command1_DragOver(Index As Integer, Source As Control, X As Single, Y As Single, State As Integer)
NewV = Command1(Index).Caption
End Sub
Private Sub Command2_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
Command2(Index).Caption = NewV
End Sub
Private Sub Command3_Click()
Dim myval(1 To 5) As Integer
Dim i As Integer, j As Integer, k As Integer
Randomize
myval(1) = Int(5 * Rnd + 1)
j = 2
Do
k = Int(5 * Rnd + 1)
For i = 2 To 5
If k = myval(i - 1) Then Exit For
Next
If i = 6 Then myval(j) = k: j = j + 1
Loop Until j = 6
For i = 1 To 5: Command1(i).Caption = myval(i): Next
End Sub
我来回复