使用随机类的随机数

时间:2012-11-04 19:55:46

标签: vb.net

我创建了一个获取随机数的应用程序。然而,我的IF声明似乎没有起作用,我不断提出重复的数字。

代码是:

Public Class Form1

Dim number As Integer
Dim star As Integer
Dim getNumber As Random = New Random()
Dim getStar As Random = New Random()

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    If (number1.Text = number2.Text) Or (number1.Text = number3.Text) Or (number1.Text = number4.Text) Or (number1.Text = number5.Text) Then
        If (number2.Text = number1.Text) Or (number2.Text = number3.Text) Or (number2.Text = number4.Text) Or (number2.Text = number5.Text) Then
            If (number3.Text = number1.Text) Or (number3.Text = number2.Text) Or (number3.Text = number4.Text) Or (number3.Text = number5.Text) Then
                If (number4.Text = number1.Text) Or (number4.Text = number2.Text) Or (number4.Text = number3.Text) Or (number4.Text = number5.Text) Then
                    If (number5.Text = number1.Text) Or (number5.Text = number2.Text) Or (number5.Text = number3.Text) Or (number5.Text = number4.Text) Then
                        number5.Text = CStr(getNumber.Next(1, 50))
                        number4.Text = CStr(getNumber.Next(1, 50))
                        number3.Text = CStr(getNumber.Next(1, 50))
                        number2.Text = CStr(getNumber.Next(1, 50))
                        number1.Text = CStr(getNumber.Next(1, 50))
                    Else
                        number5.Text = ""
                    End If
                Else
                    number4.Text = ""
                End If
            Else
                number3.Text = ""
            End If
        Else
            number2.Text = ""
        End If
    Else
        number1.Text = ""
    End If

    If (star1.Text = star2.Text) Then
        If (star2.Text = star1.Text) Then
            star1.Text = CStr(getStar.Next(1, 11))
            star2.Text = CStr(getStar.Next(1, 11))
        Else
            star1.Text = ""
            star2.Text = ""
        End If
    End If

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    number1.Clear()
    number2.Clear()
    number3.Clear()
    number4.Clear()
    number5.Clear()
    star1.Clear()
    star2.Clear()

End Sub
End Class

有人可以帮我解决这个问题吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您在设置值之前检查匹配项,因此代码不会查看设置的值。

首先设置值,然后循环,直到找到未使用的值:

number1.Text = CStr(getNumber.Next(1, 50))
Do
  number2.Text = CStr(getNumber.Next(1, 50))
Loop While number1.Text = number2.Text
Do
  number3.Text = CStr(getNumber.Next(1, 50))
Loop While number1.Text = number3.Text Or number2.Text = number3.Text
Do
  number4.Text = CStr(getNumber.Next(1, 50))
Loop While number1.Text = number4.Text Or number2.Text = number4.Text Or number3.Text = number4.Text
Do
  number5.Text = CStr(getNumber.Next(1, 50))
Loop While number1.Text = number5.Text Or number2.Text = number5.Text Or number3.Text = number5.Text Or number4.Text = number5.Text