[.net] Guessing Game using Random.Next in .NET

Started by
33 comments, last by Machaira 18 years, 1 month ago
I need to create a guessing game that generates a random number that the user has to guess. The user gets 10 chances to guess the number, and the computer tells the user if to guess lower or higher based on their guess and the actual number. If the user guess correctly the application should display a "congratualtions!" message. If the user does not guess the number within the amount of tries, the application should display the random number. Here is what I have so far... Private Sub uiGuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiGuessButton.Click Dim number As Integer Dim guess As Integer Dim randomGenerator As New Random 'generator Dim count As Integer ' counter Try 'get the number guessed guess = Convert.ToInt32(Me.uiGuessTextBox.Text) 'verify that the user entered a number Do While guess <> number number = randomGenerator.Next(1, 100) Do While count <= 10 'update counter count = count + 1 Loop Loop If guess > number Then 'display the guessed number in the label Me.uiNumberYouGuessed.Text = Convert.ToString(guess) 'display the guess needs to be lower Me.UiDisplayResultsLabel.Text = Convert.ToString("Please guess a lower number.") ElseIf guess < number Then 'display the guessed number in the label Me.uiNumberYouGuessed.Text = Convert.ToString(guess) 'display the guess needs to be higher Me.UiDisplayResultsLabel.Text = Convert.ToString("Please guess a higher number.") ElseIf guess = number Then 'display the guessed number in the label Me.uiNumberYouGuessed.Text = Convert.ToString(guess) 'tell the user they guessed correctly Me.UiDisplayResultsLabel.Text = Convert.ToString("Congratulations!") Else 'display try again Me.UiDisplayResultsLabel.Text = Convert.ToString("Try again.") End If 'after 10 tries, display the random number Me.UiDisplayResultsLabel.Text = Convert.ToString("The number was " & number & ".") Catch exformat As FormatException MessageBox.Show("The number must be numeric.", "Guessing Game", _ MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.Message, "Guessing Game", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Try Something is not in the right order her, because my code is giving me the answer after the first guess...
Advertisement
I don't know if this is homework or not, so I'll just give a hint.

Do While count <= 10
'update counter
count = count + 1
Loop

The problem lies with that code.
Yes, this is homework.
Does that Do while loop not need to be nested? Or do I have it in the wrong place and that is why I am terminating the program too soon? Thanks!
if you have a loop inside another loop, the inside loop has to exit before the outer loop continues.
OH YEAH! I remember learning that! LOL! That would be the inner loop contined within the outer loop, which must be true for the loops to be nested and to work correctly.

Hmmmmmm...
I don't need a nested loop do I?
Hey vanidosa27,

There are a lot of logic errors in you code which makes it behave other than what you want it to do. Since it is a homework, I will throw you hints so you have something to think about.

- Notice that the number to guess is recalculated every time the button is clicked, so is the counter.

- "Do While guess <> number" at this point number was only declared... so it's 0!

- Think what Nypyren said

- Your condition " If guess > number ", do you see that it will always be executed only once?

- Notice that the lines
 Else'display try againMe.UiDisplayResultsLabel.Text = Convert.ToString("Try again.")End If

will never be executed because you already have smaller than, larger than, or equal.
I think you need to rethink your code.

Try writing the program down logically on paper and the program will stare you right in the face. Now just a little hint I wouldn't write the entire logic of the game in a button click handler. Good luck



- Notice that the number to guess is recalculated every time the button is clicked, so is the number of tries.

I can't seem to figure this one out

- "Do While guess <> number" at this point number was only declared... so it's 0!

Okay, so I need to initialize my number

- Think what Nypyren said

- Your condition " If guess > number ", do you see that it will always be executed only once?

No, I don't see this either

- Notice that the lines

Else
'display try again
Me.UiDisplayResultsLabel.Text = Convert.ToString("Try again.")
End If

I think I understand this one, I need to take out the =, or move it to the else statement?


To Adam, I asked my teacher if I should use a Call method and she said that a user-defined sub procedure should only be used when two or more objects needs to call it?
Pseudocode

1. declare variables
2. Generate a random number
3. convert the guess to and integer
4. Compare the guess with the random number
5. count the guess
6. tell the user if the guess is higher or lower
7. end after 10 guesses or correct answer

This topic is closed to new replies.

Advertisement