My Visual Basic program isn't properly comparing my input guess with the random number answer, I get correct no matter what I input. Can you check my code to find the issue? I am attaching the program requirements in a screenshot of a Word document.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

My Visual Basic program isn't properly comparing my input guess with the random number answer, I get correct no matter what I input. Can you check my code to find the issue? I am attaching the program requirements in a screenshot of a Word document.

Public Class GuessMyNumberGame

Dim counter As Integer = 0
Dim correctguessed As Integer = 0
Dim total As Integer = 0
Dim answer As Integer
Dim guess As Integer = CInt(number.Text)
Dim r As System.Random = New System.Random()


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
answer = r.Next(1, 10)


End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles AcceptButton.Click
If (counter <> 5) Then
If Not Single.TryParse(number.Text, answer) Then
MessageBox.Show("You must enter a numeric value here.", "Bad value",
MessageBoxButtons.OK, MessageBoxIcon.Information)
number.Text = ""
number.Focus()
Exit Sub

Else

If guess = answer Then
correctguessed = correctguessed + 1
MessageBox.Show("Correct!", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Information)
number.Text = ""
number.Focus()

Else
MessageBox.Show("Incorrect", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Information)
number.Text = ""
number.Focus()

End If
total = total + 1
counter = counter + 1
End If
Else
MessageBox.Show("Guess limit has been exceeded. I have a new number to guess.", "Start New Game",
MessageBoxButtons.OK, MessageBoxIcon.Information)
answer = r.Next(1, 10)
correctguessed = 0
total = 0
counter = 0
number.Text = ""
number.Focus()
End If

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles startButton.Click
MessageBox.Show("I have a new number to guess.", "Start New Game",
MessageBoxButtons.OK, MessageBoxIcon.Information)
number.Text = ""
answer = r.Next(1, 10)
counter = 0
number.Focus()

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles exitButton.Click
number.Text = ""

Close()

End Sub
End Class

Chapter 5 - Case 1
Guess My Number Game
Step 6 - Add code inthe Check My Guess button's Click event to check the guess and displaythe
results:
Guess My Number
Afterthe validation code, add codeto detemine ifthe guess was comrect. Displaya message indicating ifthe
guess was corect or not. If corect, add 1 to the counter for comect guesses. Add 1 to the guess limit and to the
total guesses counters whetherthe guess wascorrect or not.
I'm thinking of a number between 1
and 10. Enter your guess and click
on the Check My Guess button.
Check the guess limit counterto determine ifthe userhas completed five guesses. If so, display a message
telling the userthe limit has been exceeded, and that the program has a new number to guess. Generate a new
random number, and then reset the guess limit counter.
Your guess:
Be sure to clearthe guess textbox after each guess and put the cursorthere to make it quick and easy forthe
userto enter another guess.
Check My Guess
Start a New Game
Exit
Step 7 - Add code inthe Start New Game button's Click event to start a new game:
Figure 1
Add code to reset the guess limit counter. Generate a new random number, and display a message box telling
the userthat the program has a new number to guess. Clearthe guess textboxand put the cursor in it.
Instructions
In this case, you will create a Msual Basic solution that plays a number guessing game with the user. The
program will randomly generate a value between one and ten, and will compare the user's guess to this value.
The program will give the user five attempts to guess the correctnumber. When the user exits the game, the
program will displaythe peroentage of guesses that were comrect. This program uses counters and accumulators.
Be sure to add the code forthe Clear button and the Exit button.
Step 8: Save and run
Save all files, then start the application. Test the program using various input values. See if you can guess the
number!
Step 1: Create the Project:
Create a Msual Basic Project using the project name "GuessMxNuoker".
Step 2 - Design the Form:
Design the form as shown in Figure 1. You will need three button controls, one text box, and two label controls.
Set the Check My Guess button to be the ecceptButton.on the form.
Step 3 - Declare requiredvariables:
You will need to declare four variables to be used as follows:
1. An integer counter variable forthe guess limit.
2. An integer accumulator variable to accumulate the number of correct guesses.
3. An integer accumulator variable to accumulate the total number of guesses.
4. An integer variable to store the randomly generated answer value
Because these variables must hold their value throughout the time the program is running, declare them as
module-level variables in the Fom's Declarations area.
Step 4 -Add code inthe Form's Loadevent to generate an iritial random value
In the Form's Load event, write code to initialize the random object and to generate a random number between 1
and 10. Store the generated number in the answer variable declared above.
Step 5 - Add code inthe Check My Guess button's Click event to validate the user input:
Usethe JrParsemethod to validate that the user entered a numeric value forthe guess. Ifthe textbox value is
not numeric, displaya message box explaining the error, then put the focus into the textboxwith the error. Use
the Exit Sub statement to leave the Click event ifthere wasan error. Your validation code for score textboxes
should be in this form:
If Not Single. TryParse (gue s:TextBox. Te xt, gues s) Then
MessageBox. Shoo ("Y ou must enter a mumeric value here.", "Bad value", -
Me ssage BoxButt ons. OK, Me ss age BoxI con.Infomation)
gue ssTextBox. Forus ()
Exit Sub
End If
Transcribed Image Text:Chapter 5 - Case 1 Guess My Number Game Step 6 - Add code inthe Check My Guess button's Click event to check the guess and displaythe results: Guess My Number Afterthe validation code, add codeto detemine ifthe guess was comrect. Displaya message indicating ifthe guess was corect or not. If corect, add 1 to the counter for comect guesses. Add 1 to the guess limit and to the total guesses counters whetherthe guess wascorrect or not. I'm thinking of a number between 1 and 10. Enter your guess and click on the Check My Guess button. Check the guess limit counterto determine ifthe userhas completed five guesses. If so, display a message telling the userthe limit has been exceeded, and that the program has a new number to guess. Generate a new random number, and then reset the guess limit counter. Your guess: Be sure to clearthe guess textbox after each guess and put the cursorthere to make it quick and easy forthe userto enter another guess. Check My Guess Start a New Game Exit Step 7 - Add code inthe Start New Game button's Click event to start a new game: Figure 1 Add code to reset the guess limit counter. Generate a new random number, and display a message box telling the userthat the program has a new number to guess. Clearthe guess textboxand put the cursor in it. Instructions In this case, you will create a Msual Basic solution that plays a number guessing game with the user. The program will randomly generate a value between one and ten, and will compare the user's guess to this value. The program will give the user five attempts to guess the correctnumber. When the user exits the game, the program will displaythe peroentage of guesses that were comrect. This program uses counters and accumulators. Be sure to add the code forthe Clear button and the Exit button. Step 8: Save and run Save all files, then start the application. Test the program using various input values. See if you can guess the number! Step 1: Create the Project: Create a Msual Basic Project using the project name "GuessMxNuoker". Step 2 - Design the Form: Design the form as shown in Figure 1. You will need three button controls, one text box, and two label controls. Set the Check My Guess button to be the ecceptButton.on the form. Step 3 - Declare requiredvariables: You will need to declare four variables to be used as follows: 1. An integer counter variable forthe guess limit. 2. An integer accumulator variable to accumulate the number of correct guesses. 3. An integer accumulator variable to accumulate the total number of guesses. 4. An integer variable to store the randomly generated answer value Because these variables must hold their value throughout the time the program is running, declare them as module-level variables in the Fom's Declarations area. Step 4 -Add code inthe Form's Loadevent to generate an iritial random value In the Form's Load event, write code to initialize the random object and to generate a random number between 1 and 10. Store the generated number in the answer variable declared above. Step 5 - Add code inthe Check My Guess button's Click event to validate the user input: Usethe JrParsemethod to validate that the user entered a numeric value forthe guess. Ifthe textbox value is not numeric, displaya message box explaining the error, then put the focus into the textboxwith the error. Use the Exit Sub statement to leave the Click event ifthere wasan error. Your validation code for score textboxes should be in this form: If Not Single. TryParse (gue s:TextBox. Te xt, gues s) Then MessageBox. Shoo ("Y ou must enter a mumeric value here.", "Bad value", - Me ssage BoxButt ons. OK, Me ss age BoxI con.Infomation) gue ssTextBox. Forus () Exit Sub End If
Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Study of Characters
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education