vb.netvisual-studioloopsvisual-studio-2015

Visual Basic Random Number Guessing Game


I need some help for an assignment in class. I'm trying to create a random number guessing game that must have a loop. Basically, you input a number as a guess and the program will display a message telling you whether the guess is too high, too low, or correct, and it will loop until you get the guess correct. The program also counts the number of guesses you've made.

Here is the form:

And here is the code I have so far:

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim decNumber As Decimal
    Dim rand As New Random
    Dim decGuess As Decimal
    Dim intCount As Integer = 0

    decGuess = CDec(txtGuess.Text)

    Do
        decNumber = rand.Next(1, 100)

        intCount += 1
        lblCount.Text = intCount.ToString

        If decNumber < decGuess Then
            lblResponse.Text = "Too low, try again."
        ElseIf decNumber > decGuess Then
            lblResponse.Text = "Too high, try again."
        ElseIf decNumber = decGuess Then
            lblResponse.Text = "Correct."
        End If

    Loop Until decGuess = decNumber

End Sub

I'm not quite sure what the error in the code is but whenever I hit "Display" the guess count is a very high number and the message shown in the label is always just "Correct.", so my question is how do I fix this and write the loop correctly?

Update:

Public Class Form1

Dim intNumber As Integer
Dim rand As New Random
Dim intGuess As Integer
Dim intCount As Integer

Private Sub btnNew_Click_1(sender As Object, e As EventArgs)
    lblResponse.Text = String.Empty
    txtGuess.Clear()
    lblCount.Text = String.Empty
    intNumber = rand.Next(1, 100)
End Sub

Private Sub btnGuess_Click(sender As Object, e As EventArgs) Handles btnGuess.Click

    intGuess = CInt(txtGuess.Text)

    If intNumber < intGuess Then
        lblResponse.Text = "Too high, try again."
    ElseIf intNumber > intGuess Then
        lblResponse.Text = "Too low, try again."
    ElseIf intNumber = intGuess Then
        lblResponse.Text = "Correct."
    End If

    intCount += 1
    lblCount.Text = intCount.ToString

End Sub

Solution

  • There are quite a few errors with your code:

    1. The definition of your random number

    You are generating the random number every loop, meaning that rather than there being a fixed number that you are trying to guess, the number is changing.

    Even if you are defining the random number outside the loop, the number changes every time that you click the button.

    You will need to define it as global variable outside the subroutines and only change it when you press the reset button

    2. The definition of intcount

    intcount is set to 0 every time you click the button since it is defined within the button click subroutine, which I do not think is desired.

    3. The fact that you are using a loop

    You do not need the loop at all. Unless you want your programme to enter an infinite loop and crash every time you press your button and your guess is incorrect, you should remove the loop and just use the code inside it.

    4. Wrong data types used

    decnumber should be an integer, since random.next() returns an integer value (you might also rename it to ToGuess)

    decGuess = CDec(textGuess.Text)
    

    should be

    decGuess = CInt(textGuess.Text)
    

    since if you input a decimal, you will never be able to guess the number


    The reason for the programme always outputting "correct" is because rather than you changing your guess so it is the same as the random number, the computer is 'guessing' your guess by changing the random number so that it will eventually become your guess, incrementing # of guesses every time.

    UPDATE:

    The reason that your new button is not working is because it does not handle its click. Assuming you have called your new button btnNew, try replacing:

    Private Sub btnNew_Click_1(sender As Object, e As EventArgs)
    

    with:

    Private Sub btnNew_Click_1(sender As Object, e As EventArgs) Handles btnNew.Click
    

    Next time when adding a subroutine for a buttons click, try double clicking the button in the design view in visual studio. This will automatically generate the click subroutine.

    Another problem that you have is that you have not generated your random number when your form loads. To solve this, double click on your form in designer view to generate the load subroutine, and then programmatically press the button

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnNew.PerformClick()
    End Sub
    

    also, thanks to jimi's comment, remember to set intCount to 0 when pressing btnNew