Hello. I'm new to programming and VB. Taking a introduction to computers, and one of the assignments I was given was a random number guessing game.
I wanted to create an app using a TextBox for the user's guess. and use a label that displays whether the guess is too high, too low, or correct. The problem I'm having is that I'm running into a infinite loop.
Basically, I haven't been able to figure how to pause the loop to get a new guess if the user's guess is incorrect and return to the loop. The teacher and another student suggested using an Exit statement in the loop. Other than the assignment stating to stay in the loop until a correct guess, it seems pointless when I can achieve the same effect with just using IF statements. I build in a limit of three loops to break the infinite loop. I wanted to have a few attempts in case I got it working. The follow problem has to add a label to show the number of guesses to guess correctly, which is partly why there is the GuessNumber label. The other labels are more for testing purposes.
On a side note, the application I wrote has four buttons for different ways of creating this application. 1) InputBox and If statements as the primary decision 2) TextBox and If Statements, 3) InputBox and Loop, 4) TextBox and Loop. I got the other three working. This is why the random number generation is in the form, rather then OnClick event handlers so the target number (the random number generated) doesn't reset on every button click.
Attachment 99309
It's possible the answer is beyond what has been covered in the text book and class. Thanks for any help in advance.
Quote:
Originally Posted by textbook
Code:
Public Class Form1
Dim strNumberGuess As String
Dim intNumberGuess As Integer
Dim number As New Random
Dim intNumber As Integer
Dim intCounter As Integer = 1
Dim strGuessNumber As String = "Please enter a number between 0 and 100"
Dim strGuessTitle As String = "Enter Guess Number"
Dim strRangeMessage As String = "Number must be between 0 and 100"
Dim strRangeTitle As String = "Out of Range Number Error"
Dim strNonNumeric As String = "Must enter numeric value between 0 and 100"
Dim strNonNumericTitle As String = "Non Numeric Entry Error"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
intNumber = number.Next(101) 'generate random on application load
txtNumberGuess.Focus()
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Me.Close()
End Sub
Private Sub cmdInputBoxLoopGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInputBoxLoopGuess.Click
[omitted code]
Private Sub cmdTextLoopGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTextLoopGuess.Click
' intNumber = 50 'test if loop for true condition
strNumberGuess = txtNumberGuess.Text
lblNumber.Text = intNumber.ToString("n0")
If Integer.TryParse(strNumberGuess, intNumberGuess) Then
If intNumberGuess >= 0 And intNumberGuess <= 100 Then
lblUserGuess.Text = intNumberGuess.ToString()
'========================================================
'compare user guess to random number
'========================================================
Do Until intNumberGuess = intNumber Or intCounter = 3 ' 3 limit to prevent an endless loop
If intNumberGuess < intNumber Then
lblAnswer.Text = "too low, try again"
txtNumberGuess.Clear()
txtNumberGuess.Focus()
strNumberGuess = txtNumberGuess.Text
ElseIf intNumberGuess > intNumber Then
lblAnswer.Text = "too high, try again"
txtNumberGuess.Clear()
txtNumberGuess.Focus()
strNumberGuess = txtNumberGuess.Text
End If
intCounter += 1
Loop
lblAnswer.Text = "correct"
lblGuessNumber.Text = intCounter.ToString("n0")
Else
MessageBox.Show(strRangeMessage, strRangeTitle)
txtNumberGuess.Clear()
txtNumberGuess.Focus()
End If
Else
MessageBox.Show(strNonNumeric, strNonNumericTitle)
txtNumberGuess.Clear()
txtNumberGuess.Focus()
End If
End Sub
Private Sub cmdInputBoxIfGuess_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInputBoxIfGuess.Click
[omitted code]
Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReset.Click
txtNumberGuess.Clear()
lblAnswer.Text = String.Empty
lblGuessNumber.Text = String.Empty
lblNumber.Text = String.Empty
lblUserGuess.Text = String.Empty
intCounter = 0
intNumber = number.Next(101)
End Sub
Private Sub cmdTextIfGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTextIfGuess.Click
[omitted code]
End Class
On a side note, the application I wrote has four buttons for different ways of creating this application. 1) InputBox and If statements as the primary decision 2) TextBox and If Statements, 3) InputBox and Loop, 4) TextBox and Loop. I got the other three working. This is why the random number generation is in the form, rather then OnClick event handlers so the target number (the random number generated) doesn't reset on every button click.
Attachment 99309
It's possible the answer is beyond what has been covered in the text book and class. Thanks for any help in advance.