Q4: Write a program that generates a random number between 1 and 100, the user n
ID: 3733619 • Letter: Q
Question
Q4: Write a program that generates a random number between 1 and 100, the user need to guess the mumber. 5 marks .If the user input a number higher than the number display a message: (too high) .If the user input a number lower than the number display a message: ( too low ) If the user input the exact number display a message:(correct) .The user can try several times until he guesses the number. l Guess the Numbert I have a number between 1 and 100 Can you guess my number? Enter Guess New Game ResultExplanation / Answer
(Assuming that the user knows the basics of how to create a Windows Form Application using VB)
Refer to the program logic below:
Create a windows form with the following control elements in Visual studio:
1. GuessInput as the name for the input field (for the user to guess the random number), along with the label 'Guess:'.
2. Readonly textbox (to display the result),along with the label 'Result:'. Let the name for the field be result.
3. Enter button, beside the GuessInput field. This button should be enabled by default i.e., when the GuessInput field has an input ready. (Use ReadOnly property for the same, i.e., ReadOnly is set to true when you want to disable any field). Let the name for the field be enter.
4. New Game button - Requirement for this button is not clearly mentioned. But, can be used to refresh/ clear the form fields' inputs.This button is enabled only when the result field has the output displayed in it.Let the name for the field be NewGame.
5. Set prompt 'I have a number between 1 and 100. Can you guess my number?' as a label.
Logic for enter button click (i.e., Enter_Click)and NewGame button click (i.e., NewGame_Click) is as follows:
Public Class Form1
Private Sub Enter_Click(ByVal sender as System.Object,ByVal e as System.EventArgs)
Dim rm As New Random
Dim result As Short = rm.next(1,100)
Dim answer As String = String.Empty
If (CShort(GuessInput.text) = result) Then
answer = "Correct!"
ElseIf (CShort(GuessInput.text) < 1) Then
answer = "Too low..."
ElseIf (CShort(GuessInput.text) > 100) Then
answer = "Too high..."
End If
result.text = answer
NewGame.ReadOnly = false
Enter.Readonly = true
End Sub
Private Sub NewGame_Click(ByVal sender as System.Object,ByVal e as System.EventArgs)
result.text = "";
GuessInput.text = ""
NewGame.ReadOnly = true
Enter.Readonly = false
End Sub
End Class
Note: This is just the program logic for the problem. This isn't executed on any IDE.
Hope this was helpful. All the best :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.