vb姓名随机排序
2,然后用VB的随机函数,产生随机序列(包括0,1,2,3)
例如:这次产生的是:2,3,1,0,把这个序列作为文本框的下标序列:
那么就按照:Text1(2)、Text1(3)、Text1(1)、Text1(0),来显示
能给我写下代码吗?数组我们还没学到,但是现在想做一个这个程序
启动新工程,在窗体上添加一个文本框Text1,在属性窗口设置它的 Index 属性为0,然后选中窗体上的这个控件,[复制] -- [粘贴],这样,就在窗体上复制出一共4个文本框,在这四个文本框里输入:
Text1-1、Text1-2、Text1-3、Text1-4。
窗体上添加一个按钮:
见下效果图:
代码如下:
Option Explicit
Private Sub Command1_Click()
Dim d(0 To 3) As Integer
Dim i As Integer
Dim n As Integer
Dim x As Integer
'产生随机序列,保存到数组d里
n = -1
Randomize
Do While n < 3
x = Int(4 * Rnd) '产生0-3之间的整数
For i = 0 To n
If x = d(i) Then
Exit For
End If
Next i
If i > n Then
n = n + 1
d(n) = x
End If
Loop
'显示结果
For i = 0 To 3
Print Text1(d(i)).Text
Next i
End Sub
Option Base 1
Private Sub Command1_Click()
Text1.SetFocus
Randomize
Dim A(4) As String
Dim B As Integer, C As Integer, D As Integer, E As Integer
Do
B = Int(Rnd * 4 + 1)
C = Int(Rnd * 4 + 1)
D = Int(Rnd * 4 + 1)
E = Int(Rnd * 4 + 1)
If B <> C And B <> D And B <> E And C <> D And C <> E And D <> E Then Exit Do
Loop
A(1) = Text1.Text
A(2) = Text2.Text
A(3) = Text3.Text
A(4) = Text4.Text
Print A(B), A(C), A(D), A(E)
End Sub