Need help with random number program

Hello! I am writing a program that generates two random numbers, outputs them to a form's label boxes and then asks the user to enter an answer. The answer is checked and if correct, a "good job" message is output. If it is wrong a "try again" message is output and the user is to continue trying the problem until they get it right. I have it generating the two random numbers fine, but everything else works terribly! When I go to run the program, the form displays, but the random numbers aren't there until the "OK" button is clicked. Then it won't recognize the correct answer and continues to output the try again message. I will paste my code below. Have I put the correct code in the wrong places? I know that the code isn't right to have it repeat a wrongly answered program, but first want to fix the display problem. Thanks for any help you can give me!

Linda

Here is my code:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents InputBox As System.Windows.Forms.TextBox
Friend WithEvents CalculateButton As System.Windows.Forms.Button
Friend WithEvents OutputBox As System.Windows.Forms.Label
Friend WithEvents value1 As System.Windows.Forms.Label
Friend WithEvents value2 As System.Windows.Forms.Label
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.value1 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.value2 = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.Label6 = New System.Windows.Forms.Label()
Me.InputBox = New System.Windows.Forms.TextBox()
Me.CalculateButton = New System.Windows.Forms.Button()
Me.OutputBox = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(72, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(128, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "What is"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'value1
'
Me.value1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.value1.Location = New System.Drawing.Point(48, 48)
Me.value1.Name = "value1"
Me.value1.Size = New System.Drawing.Size(32, 24)
Me.value1.TabIndex = 1
Me.value1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(96, 48)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(48, 23)
Me.Label3.TabIndex = 2
Me.Label3.Text = "times"
'
'value2
'
Me.value2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.value2.Location = New System.Drawing.Point(176, 48)
Me.value2.Name = "value2"
Me.value2.Size = New System.Drawing.Size(32, 23)
Me.value2.TabIndex = 3
'
'Label5
'
Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label5.Location = New System.Drawing.Point(216, 48)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(40, 23)
Me.Label5.TabIndex = 4
Me.Label5.Text = "?"
'
'Label6
'
Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label6.Location = New System.Drawing.Point(32, 104)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(208, 48)
Me.Label6.TabIndex = 5
Me.Label6.Text = "Please enter your answer below:"
'
'InputBox
'
Me.InputBox.Location = New System.Drawing.Point(32, 168)
Me.InputBox.Name = "InputBox"
Me.InputBox.TabIndex = 6
Me.InputBox.Text = ""
'
'CalculateButton
'
Me.CalculateButton.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.CalculateButton.Location = New System.Drawing.Point(184, 168)
Me.CalculateButton.Name = "CalculateButton"
Me.CalculateButton.TabIndex = 7
Me.CalculateButton.Text = "OK"
'
'OutputBox
'
Me.OutputBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.OutputBox.Location = New System.Drawing.Point(8, 216)
Me.OutputBox.Name = "OutputBox"
Me.OutputBox.Size = New System.Drawing.Size(272, 48)
Me.OutputBox.TabIndex = 8
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.OutputBox, Me.CalculateButton, Me.InputBox, Me.Label6, Me.Label5, Me.value2, Me.Label3, Me.value1, Me.Label1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
Dim randomObject As Random = New Random()
Dim randomNumber As Integer = randomObject.Next()

'two random numbers being multiplied
Dim number1 As Integer
Dim number2 As Integer

'user's answer input
Dim userGuess As Integer

'random numbers between 0 and 9 are generated
number1 = randomObject.Next(0, 10)
number2 = randomObject.Next(0, 10)

'assigns appropriate variable to textboxes for display
Me.value1.Text = String.Format("{0}", number1)
Me.value2.Text = String.Format("{0}", number2)

'gets the user's answer from box and converts it to the proper data type
userGuess = Convert.ToDecimal(Me.InputBox.Text)

'sends above variable values to CheckCalculation function
CheckCalculation(number1, number2, userGuess)

End Sub
Function CheckCalculation(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As String

Dim reply As String = "That is right, great job!"
Dim reply1 As String = "Sorry, please try again!"

If z = x * y Then
Me.OutputBox.Text = String.Format("{0}", reply)
Else
Me.OutputBox.Text = String.Format("{0}", reply1)

End If

End Function

End Class


Comments

  • : Hello! I am writing a program that generates two random numbers, outputs them to a form's label boxes and then asks the user to enter an answer. The answer is checked and if correct, a "good job" message is output. If it is wrong a "try again" message is output and the user is to continue trying the problem until they get it right. I have it generating the two random numbers fine, but everything else works terribly! When I go to run the program, the form displays, but the random numbers aren't there until the "OK" button is clicked. Then it won't recognize the correct answer and continues to output the try again message. I will paste my code below. Have I put the correct code in the wrong places? I know that the code isn't right to have it repeat a wrongly answered program, but first want to fix the display problem. Thanks for any help you can give me!
    :
    : Linda
    :
    : Here is my code:
    :
    : Public Class Form1
    : Inherits System.Windows.Forms.Form
    :
    : #Region " Windows Form Designer generated code "
    :
    : Public Sub New()
    : MyBase.New()
    :
    : 'This call is required by the Windows Form Designer.
    : InitializeComponent()
    :
    : 'Add any initialization after the InitializeComponent() call
    :
    : End Sub
    :
    : 'Form overrides dispose to clean up the component list.
    : Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    : If disposing Then
    : If Not (components Is Nothing) Then
    : components.Dispose()
    : End If
    : End If
    : MyBase.Dispose(disposing)
    : End Sub
    :
    : 'Required by the Windows Form Designer
    : Private components As System.ComponentModel.IContainer
    :
    : 'NOTE: The following procedure is required by the Windows Form Designer
    : 'It can be modified using the Windows Form Designer.
    : 'Do not modify it using the code editor.
    : Friend WithEvents Label1 As System.Windows.Forms.Label
    : Friend WithEvents Label3 As System.Windows.Forms.Label
    : Friend WithEvents Label5 As System.Windows.Forms.Label
    : Friend WithEvents Label6 As System.Windows.Forms.Label
    : Friend WithEvents InputBox As System.Windows.Forms.TextBox
    : Friend WithEvents CalculateButton As System.Windows.Forms.Button
    : Friend WithEvents OutputBox As System.Windows.Forms.Label
    : Friend WithEvents value1 As System.Windows.Forms.Label
    : Friend WithEvents value2 As System.Windows.Forms.Label
    : Private Sub InitializeComponent()
    : Me.Label1 = New System.Windows.Forms.Label()
    : Me.value1 = New System.Windows.Forms.Label()
    : Me.Label3 = New System.Windows.Forms.Label()
    : Me.value2 = New System.Windows.Forms.Label()
    : Me.Label5 = New System.Windows.Forms.Label()
    : Me.Label6 = New System.Windows.Forms.Label()
    : Me.InputBox = New System.Windows.Forms.TextBox()
    : Me.CalculateButton = New System.Windows.Forms.Button()
    : Me.OutputBox = New System.Windows.Forms.Label()
    : Me.SuspendLayout()
    : '
    : 'Label1
    : '
    : Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.Label1.Location = New System.Drawing.Point(72, 8)
    : Me.Label1.Name = "Label1"
    : Me.Label1.Size = New System.Drawing.Size(128, 23)
    : Me.Label1.TabIndex = 0
    : Me.Label1.Text = "What is"
    : Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    : '
    : 'value1
    : '
    : Me.value1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.value1.Location = New System.Drawing.Point(48, 48)
    : Me.value1.Name = "value1"
    : Me.value1.Size = New System.Drawing.Size(32, 24)
    : Me.value1.TabIndex = 1
    : Me.value1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    : '
    : 'Label3
    : '
    : Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.Label3.Location = New System.Drawing.Point(96, 48)
    : Me.Label3.Name = "Label3"
    : Me.Label3.Size = New System.Drawing.Size(48, 23)
    : Me.Label3.TabIndex = 2
    : Me.Label3.Text = "times"
    : '
    : 'value2
    : '
    : Me.value2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.value2.Location = New System.Drawing.Point(176, 48)
    : Me.value2.Name = "value2"
    : Me.value2.Size = New System.Drawing.Size(32, 23)
    : Me.value2.TabIndex = 3
    : '
    : 'Label5
    : '
    : Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.Label5.Location = New System.Drawing.Point(216, 48)
    : Me.Label5.Name = "Label5"
    : Me.Label5.Size = New System.Drawing.Size(40, 23)
    : Me.Label5.TabIndex = 4
    : Me.Label5.Text = "?"
    : '
    : 'Label6
    : '
    : Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.Label6.Location = New System.Drawing.Point(32, 104)
    : Me.Label6.Name = "Label6"
    : Me.Label6.Size = New System.Drawing.Size(208, 48)
    : Me.Label6.TabIndex = 5
    : Me.Label6.Text = "Please enter your answer below:"
    : '
    : 'InputBox
    : '
    : Me.InputBox.Location = New System.Drawing.Point(32, 168)
    : Me.InputBox.Name = "InputBox"
    : Me.InputBox.TabIndex = 6
    : Me.InputBox.Text = ""
    : '
    : 'CalculateButton
    : '
    : Me.CalculateButton.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.CalculateButton.Location = New System.Drawing.Point(184, 168)
    : Me.CalculateButton.Name = "CalculateButton"
    : Me.CalculateButton.TabIndex = 7
    : Me.CalculateButton.Text = "OK"
    : '
    : 'OutputBox
    : '
    : Me.OutputBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    : Me.OutputBox.Location = New System.Drawing.Point(8, 216)
    : Me.OutputBox.Name = "OutputBox"
    : Me.OutputBox.Size = New System.Drawing.Size(272, 48)
    : Me.OutputBox.TabIndex = 8
    : '
    : 'Form1
    : '
    : Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    : Me.ClientSize = New System.Drawing.Size(292, 273)
    : Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.OutputBox, Me.CalculateButton, Me.InputBox, Me.Label6, Me.Label5, Me.value2, Me.Label3, Me.value1, Me.Label1})
    : Me.Name = "Form1"
    : Me.Text = "Form1"
    : Me.ResumeLayout(False)
    :
    : End Sub
    :
    : #End Region
    :
    : Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
    : Dim randomObject As Random = New Random()
    : Dim randomNumber As Integer = randomObject.Next()
    :
    : 'two random numbers being multiplied
    : Dim number1 As Integer
    : Dim number2 As Integer
    :
    : 'user's answer input
    : Dim userGuess As Integer
    :
    : 'random numbers between 0 and 9 are generated
    : number1 = randomObject.Next(0, 10)
    : number2 = randomObject.Next(0, 10)
    :
    : 'assigns appropriate variable to textboxes for display
    : Me.value1.Text = String.Format("{0}", number1)
    : Me.value2.Text = String.Format("{0}", number2)
    :
    : 'gets the user's answer from box and converts it to the proper data type
    : userGuess = Convert.ToDecimal(Me.InputBox.Text)
    :
    : 'sends above variable values to CheckCalculation function
    : CheckCalculation(number1, number2, userGuess)
    :
    : End Sub
    : Function CheckCalculation(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As String
    :
    : Dim reply As String = "That is right, great job!"
    : Dim reply1 As String = "Sorry, please try again!"
    :
    : If z = x * y Then
    : Me.OutputBox.Text = String.Format("{0}", reply)
    : Else
    : Me.OutputBox.Text = String.Format("{0}", reply1)
    :
    : End If
    :
    : End Function
    :
    : End Class
    :
    :
    :
    Linda, its actually simple. You have your code in the on-click method of your button. That means the code won't execute until you press the button. After that you're a step behind. Everytime you place your answer in the box (the correct answer), You click the button, recalculate your numbers, then compare the answer you input. But the numbers have now changed. Might try a sub to set up the problem, call it from the on-activate, then do your checking in button on_click and recall the setup function after getting correct feedback. You can keep or clear the inputbox for incorrect answer.

  • Thanks for the help! I think I have it now.....

    Linda
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories