borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (program on server)Next Topic (vb code) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Need help with random number mult. program
Poster Message
Lindajt
Level: Guest


icon Need help with random number mult. program

Hello!   I have been assigned to write a simple multiplication problem (see code below) The program should display two randomly generated numbers and ask the user to enter an answer and click "ok".  If the answer is right, it should display a "good job" message and a new problem.  If the user is wrong "try again" is displayed and the problem doesn't change until they get it right. Everything compiles fine, but when it runs the trouble begins!  The form will come up with none of the randomly generated numbers on it.  AFTER I click the "ok" button, it will show the numbers, but then I continue to get the wrong output messages.  I know I need to fix the CheckCalculation function and also put a while loop around  Private Sub CalculateButton_Click to make it continue on a wrong answer, but I am not sure just how to do that either! Any help would be greatly appreciated!!!!

Thanks,  Linda

#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
    <System.Diagnostics.DebuggerStepThrough()> 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, 112)
        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(8, 17)
        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.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        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

        'converts user's guess to a double
        userGuess = Convert.ToDouble(Me.InputBox.Text)

        '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)

        '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

05-11-2002 at 03:52 PM
| Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1617
icon Re: Need help with random number mult. program

I don't have VB.Net to give you code example; however...

In the CalculateButton_Click you display the random numbers, this should be put in whatever the Form_Load Equilvant is.

If CheckCalculation is boolean, you could do something like the following:

If CheckCalculation true
' Put another routine in to get random numbers
End If

Something Like (although can't verify)


' Remember to Put the GetRandNumber in the Form_Load Equilvant Also

Private Sub GetRandNumber()
        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
        '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)
End Sub

Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
        'user's answer input
        Dim userGuess As Integer

        'converts user's guess to a double
        userGuess = Convert.ToDouble(Me.InputBox.Text)

        'sends above variable values to CheckCalculation function
        If CheckCalculation(number1, number2, userGuess) Then
             GetRandNumber
        End If
    End Sub

    Function CheckCalculation(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Boolean

        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)
            CheckCalculation=True
        Else
            Me.OutputBox.Text = String.Format("{0}", reply1)
            CheckCalculation=False
        End If

    End Function



____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

05-11-2002 at 08:16 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : VB.Net : Need help with random number mult. program
Previous Topic (program on server)Next Topic (vb code) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder