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.
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.
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.
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()
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
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
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
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.