-1

我创建了一个获取随机数的应用程序。但是,我的 IF 语句似乎不起作用,并且我不断提出重复的数字。

代码是:

公开课形式1

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

谁能帮我解决这个问题?

谢谢

4

1 回答 1

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
于 2012-11-04T20:11:02.293 回答