0% found this document useful (0 votes)
57 views

Guess My Number Code

This code creates a "Guess My Number" game that allows the user to guess a randomly generated number. It contains procedures to generate a random number, check the user's guess, and update the display with feedback on the number of guesses and whether the guess was too high or too low. The user can choose between guessing a number from 1-9, 1-99, or 1-999 and the code tracks the number of guesses taken using a count variable.

Uploaded by

api-307933836
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Guess My Number Code

This code creates a "Guess My Number" game that allows the user to guess a randomly generated number. It contains procedures to generate a random number, check the user's guess, and update the display with feedback on the number of guesses and whether the guess was too high or too low. The user can choose between guessing a number from 1-9, 1-99, or 1-999 and the code tracks the number of guesses taken using a count variable.

Uploaded by

api-307933836
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Guess My Number Code

Public Class Form1


Dim ComputerNumber As Integer
Dim Count As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Count = 0
GroupBox1.Enabled = False
Button1.Enabled = False
TextBox1.Enabled = True
Button2.Enabled = True
Label1.Text = "Number of Guesses:" + Str(Count)
Label2.Text = ""
TextBox1.Focus()
TextBox1.Clear()
GetNumber()
End Sub
Private Sub GetNumber()
Randomize()
If RadioButton1.Checked = True Then
ComputerNumber = Rnd() * 9 + 1
ElseIf RadioButton2.Checked = True Then
ComputerNumber = Rnd() * 99 + 1
Else
ComputerNumber = Rnd() * 999 + 1
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
If TextBox1.Text <> "" Then
Try
If Integer.Parse(TextBox1.Text) = ComputerNumber Then
CorrectGuess()
Else
NotCorrectGuess()
End If
Catch ex As Exception
TextBox1.Clear()
TextBox1.Focus()
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub CorrectGuess()
Count += 1
Label1.Text = "Number of Guesses: " + Str(Count)
GroupBox1.Enabled = True
Button1.Enabled = True
TextBox1.Enabled = False
Button2.Enabled = False
MsgBox("You Guessed My Number In " + Str(Count) + " Guesses!")
End Sub
Private Sub NotCorrectGuess()

Count += 1
Label1.Text = "Number of Guesses: " + Str(Count)
If Integer.Parse(TextBox1.Text) < ComputerNumber Then
Label2.Text = "Your guess was too low"
Else
Label2.Text = "Your guess was too high"
End If
TextBox1.Clear()
TextBox1.Focus()
End Sub
End Class

You might also like