Question random with multiplication tables

Blade

New member
Joined
Jan 8, 2010
Messages
3
Location
Tennessee
Programming Experience
Beginner
Hi all I am by far not a programmer i just know enough to get me by doing little project and stuff for me and my family. I am at the present moment trying to write a small program to help my daughter out with her multiplication tables. My problem is i need 2 different random number values to be set at the start of the program. ie. right now she is going from 1-6 on her test but use the nubers 1-6 she goes all the way to the number 10 to multiply them a little ie 1*1 thru 1*10 all the way to 6*1 thru 6*10 so i don't need it going to 7*8 how i was able to explain that clear enough. but i need it to be set at the start due to every week they are gonna work up a number till they get to 12 here is code i have I am sure it looks rough but i at least have a work model to play with feel free to correct and give any advice as i am trying to learn .net a little better.

VB.NET:
Public Class frmmul
    Dim Answer As Integer
    Dim B As Integer
    Dim RandomClass As New Random()
    Dim RandomNumber As Integer
    Dim x As String
    Private Sub frmadd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RandomNumber = RandomClass.Next(0, 20)
        Label1.Text = RandomNumber
        Label2.Text = "X"
        Label3.Text = RandomClass.Next(1, 5)
        Answer = Val(Label1.Text * Label3.Text)
        Label4.Text = x
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RandomNumber = RandomClass.Next(0, 20)
        Label1.Text = RandomNumber
        Label2.Text = "X"
        Label3.Text = RandomClass.Next(1, 5)
        If TextBox1.Text = Answer Then x = (x + 1) Else x = x
        TextBox1.Text = ""
        TextBox1.Focus()
    End Sub
End Class
 
I see a multitude of problems with your code. I tried to fix it but it's a disaster.

First of all, place:

Option Strict On

at the very top of your program, above the Class line. This will force explicit type conversion, to avoid a lot of problems. Use the TryParse method to convert strings to integers. Examples:

Dim num1, num2 As Integer
Dim answer, reply As Integer
Integer.TryParse(Label1.Text, num1)
Integer.TryParse(Label3.Text, num2)
answer = num1 * num1
Integer.TryParse(Textbox1.Text, reply)
If reply = answer Then x = x + 1


Use the ToString method to convert integers back to strings for display. Examples:

Label1.Text = RandomClass.Next(0, 20).ToString
Label3.Text = RandomClass.Next(1, 5).ToString


You should have separate buttons for entering a new problem and checking for the correct answer. The way you have it now is very confusing. After the reply is entered, it should give feedback and then a new button click should display the new problem.

In order to vary the maximum random number, use a variable instead of the constant value. Have another textbox for input of the maximum value and use that value for the random number. Example:

Dim randnum As Integer
Integer.TryParse(Textbox2.Text, randnum)
Label3.Text = RandomClass.Next(1, randnum + 1).ToString

Note: The 2nd argument in the Next method must be 1 more than the maximum in order to include all the numbers in the range.
 
Thanks

Thank you much for you reply I done a program like this one years ago in vb just adding instead and never could figure it out. gonna sit here now and try to re write all of this will post when i get done and see what you think. Once again Thanks much.

another note: you said i should do 2 different buttons for check and get new problem I am actually trying to have it pull to the next problem and set it on a timer of 3 min. do I need to take a totally different route?
 
Ok Big changes

Well here it goes guys. I think I have it doing what I want it to do please have a look and tell me anywhere I went wrong. I have 2 forms one for selection of math function add subtract multiply divide and one for GUI also have 1 module to hold setting please let me know how i can clean it up and also I am have a problem with Divide by 0 so I had to set my lowest random to one if anybody knows a work around please let me know.

VB.NET:
Module Formulas
    '*********************Delclrations**************************************************************
    Public answer, reply, countTotal, randnum, randnumtop, num1, num2, Correct, Missed As Integer
    Public RandomClass As New Random()
    Public signchange As Integer
    '***********************************************************************************************
    Public Sub CheckAnswer_Multiply()
        '********************************************************************
        Integer.TryParse(Gui.TopNumLabel.Text, num1)
        Integer.TryParse(Gui.ConstantLabel.Text, num2)
        Integer.TryParse(Gui.TextBox1.Text, reply)
        answer = CInt(num1 * num2)
        If answer = reply Then Correct = Int(Correct) + 1
        If Int(answer) <> Int(reply) Then MsgBox(CStr(answer), MsgBoxStyle.Information, Title:=("Wrong"))
        Gui.RightLabel.Text = Int(Correct) & " Out of"
        Gui.TotalLabel.Text = (countTotal) & "Correct"
    End Sub
    Public Sub CheckAnswer_Add()
        Integer.TryParse(Gui.TopNumLabel.Text, num1)
        Integer.TryParse(gui.ConstantLabel.Text, num2)
        Integer.TryParse(gui.TextBox1.Text, reply)
        answer = CInt(num1 + num2)
        If Int(answer) = Int(reply) Then Correct = Int(Correct) + 1
        If Int(answer) <> Int(reply) Then MsgBox(CStr(answer), MsgBoxStyle.Information, Title:=("Wrong"))
        Gui.RightLabel.Text = CStr(Int(Correct)) & " Out of"
        gui.TotalLabel.Text = CStr(countTotal) & "Correct"
    End Sub
    Public Sub CheckAnswer_subtract()
        Integer.TryParse(Gui.TopNumLabel.Text, num1)
        Integer.TryParse(Gui.ConstantLabel.Text, num2)
        Integer.TryParse(Gui.TextBox1.Text, reply)
        answer = CInt(num1 - num2)
        If Int(answer) = Int(reply) Then Correct = Int(Correct) + 1
        If Int(answer) <> Int(reply) Then MsgBox(CStr(answer), MsgBoxStyle.Information, Title:=("Wrong"))
        Gui.RightLabel.Text = CStr(Int(Correct)) & " Out of"
        Gui.TotalLabel.Text = CStr(countTotal) & "Correct"
    End Sub
    Public Sub CheckAnswer_Divide()
        Integer.TryParse(Gui.TopNumLabel.Text, num1)
        Integer.TryParse(Gui.ConstantLabel.Text, num2)
        Integer.TryParse(Gui.TextBox1.Text, reply)
        answer = (num1 \ num2)
        If Int(answer) = Int(reply) Then Correct = Int(Correct) + 1
        If Int(answer) <> Int(reply) Then MsgBox(CStr(answer), MsgBoxStyle.Information, Title:=("Wrong"))
        Gui.RightLabel.Text = CStr(Int(Correct)) & " Out of"
        Gui.TotalLabel.Text = CStr(countTotal) & "Correct"
    End Sub
End Module

VB.NET:
Option Strict On
Public Class Gui
    Inherits System.Windows.Forms.Form
    Public Sub Getnew()
        TextBox1.Focus()
        TextBox1.Text = ""
        TopNumLabel.Text = RandomClass.Next(1, randnumtop + 1).ToString
        ConstantLabel.Text = RandomClass.Next(1, randnum + 1).ToString
    End Sub
    Private Sub gui_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Initilize*********************************************
        If signchange = 1 Then Sign.Text = "X"
        If signchange = 2 Then Sign.Text = "+"
        If signchange = 3 Then Sign.Text = "-"
        If signchange = 4 Then Sign.Text = "/"
        Slection.Close()
        RightLabel.Hide()
        TotalLabel.Hide()
        Call Getnew()
        countTotal = 0
        Correct = 0
        Missed = 0
    End Sub
    '**************************************************************
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        countTotal = Int(countTotal) + 1
        If signchange = 1 Then Call CheckAnswer_Multiply()
        If signchange = 2 Then Call CheckAnswer_Add()
        If signchange = 3 Then Call CheckAnswer_subtract()
        If signchange = 4 Then Call CheckAnswer_Divide()
        RightLabel.Show()
        TotalLabel.Show()
        Call Getnew()
    End Sub
End Class


VB.NET:
Public Class Slection
    Inherits System.Windows.Forms.Form
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Integer.TryParse(TextBox1.Text, randnumtop)
        Integer.TryParse(TextBox2.Text, randnum)
        If RadioButton1.Checked Then signchange = (signchange + 1)
        If signchange = 1 Then Gui.Show()
        If RadioButton2.Checked Then signchange = signchange + 2
        If signchange = 2 Then Gui.Show()
        If RadioButton3.Checked Then signchange = signchange + 3
        If signchange = 3 Then Gui.Show()
        If RadioButton4.Checked Then signchange = signchange + 4
        If signchange = 4 Then Gui.Show()
    End Sub

    Private Sub Slection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolTip1.SetToolTip(TextBox1, "Enter the highest # for left side")

        Me.ToolTip1.GetToolTip(TextBox1)
    End Sub
End Class
 
Back
Top