如何在给定范围之间生成随机数?VB.NET

时间:2017-02-09 05:41:44

标签: vb.net random numbers

我尝试创建computer guess game,其中我认为1-100与计算机之间的数字生成1-100之间的随机数,并且用户会给出生成数字过低或过低的响应高或正确的guess.EG计算机生成数字23如果我提供输入L然后下次它将生成1 t0之间的数字23.如果它下次显示10然后我按H那么它会产生10到23之间的数字,希望你能理解我想要做的事情。H同样如此。 谢谢 这是我尝试的代码

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Declare Variables
    Dim strGuess As Char
    Dim random As New Random
    Dim answer As Integer
    Dim low As Integer
    Dim high As Integer
   Line1:
    MessageBox.Show("You pick a number between 0 and 100 and i will try to guess.Respond")
    answer = random.Next(1, 100)
    strGuess = InputBox("Is it " & answer, "Number Guessing Game")

    ' lstGuesses.Items.Add(strGuess)'
    low = answer
  Line2:
    If (CChar(strGuess) = CChar("L")) Then

        low = random.Next(1, low)
        strGuess = InputBox("Is it " & low, "Number Guessing Game")

    End If

    GoTo Line2

    high = answer
  Line7:
    If (CChar(strGuess) = CChar("H")) Then

        high = random.Next(high, 100)
        strGuess = InputBox("Is it " & high, "Number Guessing Game ")

    End If
    GoTo Line7
 End Sub

1 个答案:

答案 0 :(得分:2)

试试这段代码,

Public Class Form1
Dim strGuess As Char
Dim random As New Random
Dim answer As Integer
Dim low As Integer = 1
Dim high As Integer = 100
Dim Attempt As Integer = 0

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

    MessageBox.Show("You pick a number between 0 and 100 and i will try to guess.Respond")
    answer = random.Next(1, 100)
    strGuess = InputBox("Is it " & answer, "Number Guessing Game")

    If strGuess = "L" Then
        low = answer
        LowFunc(answer)
    ElseIf strGuess = "H" Then
        high = answer
        HighFunc(answer)
    ElseIf strGuess = "R" Then
        MsgBox("I guessed it in " & Attempt & " tries")
    ElseIf strGuess = "X" Then
        End
    End If

End Sub

Private Function LowFunc(ByVal answer As Integer)

    Attempt = Attempt + 1

    answer = random.Next(low, high)
    strGuess = InputBox("Is it " & answer, "Number Guessing Game")

    If strGuess = "L" Then
        low = answer
        LowFunc(answer)
    ElseIf strGuess = "H" Then
        high = answer
        HighFunc(answer)
    ElseIf strGuess = "R" Then
        MsgBox("I guessed it in " & Attempt & " tries")
    ElseIf strGuess = "X" Then
        End
    End If

    Return Nothing
End Function

Private Function HighFunc(ByVal answer As Integer)

    Attempt = Attempt + 1

    answer = random.Next(low, high)
    strGuess = InputBox("Is it " & answer, "Number Guessing Game")

    If strGuess = "H" Then
        high = answer
        HighFunc(answer)
    ElseIf strGuess = "L" Then
        low = answer
        LowFunc(answer)
    ElseIf strGuess = "R" Then
        MsgBox("I guessed it in " & Attempt & " tries")
    ElseIf strGuess = "X" Then
        End
    End If

    Return Nothing
End Function
End Class

-

在审核了您提供的此Link后,我更新了此代码。它会在highlow之间生成随机数。 highlow的值将相应地更改为输入HL,这将由玩家/用户提供,并且当正确的数字将被猜到玩家/用户应将R作为输入。他们可以提供X作为退出应用程序的输入。 Attempt将显示有多少人尝试猜测正确的数字。