我必须提出一个程序,在文本框中随机移动名称。我已经把所有的东西都变成了名字随机变换的点。它只移动一次,但是我的消息框通过代码出现的次数与名称应该在我点击确定后移动一样多次。有谁知道为什么循环不适用于名称转换。我想也许消息框需要控制循环,但我无休止地搜索,无法找到如何做到这一点。任何建议或推荐到其他网站将是非常感谢。我的代码如下。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RandomNumber As Integer
Dim min, max As Integer
Dim temp, temp2, temp3, temp4, temp5, temp6 As String
Dim i As Integer
min = 3
max = 11
Randomize()
RandomNumber = Int((max - min + 1) * Rnd() + min)
temp = n1.Text
temp2 = n2.Text
temp3 = n3.Text
temp4 = n4.Text
temp5 = n5.Text
temp6 = n6.Text
For i = 0 To RandomNumber - 1
n1.Text = temp6
n2.Text = temp
n3.Text = temp2
n4.Text = temp3
n5.Text = temp4
n6.Text = temp5
MessageBox.Show("Shift " & i & " of " & RandomNumber & " complete")
Next
End Sub
结束班
答案 0 :(得分:2)
认为临时变量也应该在循环中
For i = 0 To RandomNumber - 1
temp = n1.Text
temp2 = n2.Text
temp3 = n3.Text
temp4 = n4.Text
temp5 = n5.Text
temp6 = n6.Text
n1.Text = temp6
n2.Text = temp
n3.Text = temp2
n4.Text = temp3
n5.Text = temp4
n6.Text = temp5
//MessageBox.Show("Shift " & i & " of " & RandomNumber & " complete")
Next
您也可以使用一个临时变量
这样做For i = 0 To RandomNumber - 1
temp = n6.Text
n6.Text = n5.Text
n5.Text = n4.Text
n4.Text = n3.Text
n3.Text = n2.Text
n2.Text = n1.Text
n1.Text = temp
Next
答案 1 :(得分:0)
在MessageBox之前或之后添加此行:
Application.DoEvents
答案 2 :(得分:0)
将临时变量的赋值放在循环中就像encodebiz建议的那样可以使它工作,但你根本不需要循环。如果将字符串放在数组中,则可以计算它们的位置:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RandomNumber As Integer
Dim min, max As Integer
Dim temp As New String(5)
min = 3
max = 11
Randomize()
RandomNumber = Int((max - min + 1) * Rnd() + min)
temp(0) = n1.Text
temp(1) = n2.Text
temp(2) = n3.Text
temp(3) = n4.Text
temp(4) = n5.Text
temp(5) = n6.Text
n1.Text = temp((0 + RandomNumber) Mod 6)
n2.Text = temp((1 + RandomNumber) Mod 6)
n3.Text = temp((2 + RandomNumber) Mod 6)
n4.Text = temp((3 + RandomNumber) Mod 6)
n5.Text = temp((4 + RandomNumber) Mod 6)
n6.Text = temp((5 + RandomNumber) Mod 6)
End Sub