Multiplication Program
Multiplication Program
Multiplication Program
Multiplication Program
Multiplication Program
Multiplication Program Multiplication Program Multiplication Program Multiplication Program Multiplication Program Multiplication Program Multiplication Program Multiplication Program
Multiplication Program Multiplication Program
Multiplication Program
Go Back  Xtreme Visual Basic Talk > > > Multiplication Program


Reply
 
Thread Tools Display Modes
  #1  
Old 03-19-2012, 08:18 AM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default Multiplication Program


This is a Visual Basic 2010 Program

I am fairly new to Visual Basic, my Instructor gave me an assignment that I do not know how to finish.

He gave me some tips but I still don't know how to put them in program.

Teachers Advice:
The way it is working, you are looping 3 times per button click. Instead when the user clicks the button, do your checks and show the appropriate message boxes and increment any necessary counters.
You will need a counter to keep track of the user clicking the buttons. So if the user clicks and the answer is wrong, then increment the counter. When the user gets it right, or they hit their 3 guess maximum, then you reset that counter to the initial value (either 1 or 0, depending on how your if is coded).
You need to regenerate your random numbers after they get it right, or exceed their max guesses.

I don't know how to count the button clicks or make it allow me guess 3 times till i get it right or wrong.
I also do not know how to make the program restart when I either miss 3 times or got it right the first time.

Can anyone can give me advice or show me how to do it, Thank you so much. Sorry if I sound dumb I just don't want to fail.

Code:
Public Class Form1 
Dim num1Random As New Random()
Dim num1 As Integer = num1Random.Next(1, 11)
Dim num2 As Integer = num1Random.Next(1, 11)
Dim answer As Integer
Public Clicks As Integer
Dim Cnt As Integer
Public Sub numButton1_Click(sender As System.Object, e As System.EventArgs) Handles numButton1.Click

answer = Val(TextBox1.Text)

If answer = (num1 * num2) Then

MessageBox.Show("Correct! Time for another question")

Else

MessageBox.Show("INCORRECT! Try Again.")
If Cnt = 3 Then
MessageBox.Show("INCORRECT! Correct answer was: " & (num1 * num2) & ". Time for another question.")
numLabel.Text = ""
Cnt = 0

Exit Sub
End If
End If

End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

numLabel.Text = "What is " & num1 & " times " & num2 & " ?"

End Sub

Thanks for the Help.
Attached Files
File Type: zip TomEarlHensonAssignment1(Guessing Game).zip (77.8 KB, 0 views)

Last edited by Flyguy; 03-19-2012 at 11:26 AM. Reason: Added [code][/code] tags
Reply With Quote
  #2  
Old 03-19-2012, 08:34 AM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Firstly, this is in the wrong forum (this is old VB forum), hopefully someone will move it to .NET.

Quote:
I also do not know how to make the program restart when I either miss 3 times or got it right the first time.
This one first.

You want an Initialisation routine. Rather than having num1 and num2 initialised when the form is created, put them randomizing in the initilisation routine. Also, the numLabel.Text = "What is " & num1 & " times " & num2 & " ?" would go in the initialisation routine.

You'd then call this initialisation routine on the form loading, and it's also ready for you to use when they've guessed correctly, or they've guessed incorrectly 3 times.

Code:
I don't know how to count the button clicks or make it allow me guess 3 times till i get it right or wrong.
You've nearly got it. You've got you Cnt variable to store it, you just aren't incrementing it or anything.

The first thing you should do when they get an answer wrong is increment that value (Cnt = Cnt + 1, or Cnt += 1 achieves the same thing).

After incrementing it, you then check what the value is. If it's less than 3 then show the Incorrect message, else you show them the correct answer, set Cnt to 0 and run the new initialisation routine that was created. You could move setting Cnt = 0 to the initialisation routine, because then it would be set to 0 if they've guessed incorrectly twice but got it right the third time and guessing correctly calls the initialisation routine.

As a side note, I'd suggest taking answer from being form scope into declaring it in the Button_Click routine because that's the only place it is used.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 03-19-2012, 08:52 AM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default Thank You

I will try to fix my mistakes, If I have any errors I will post them.

Thank you so Much!
Reply With Quote
  #4  
Old 03-19-2012, 08:59 AM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

No worries. I'm reluctant to just post the code for you because you've said it is for an assignment. It'd be better for you to try and understand it rather than cutting and pasting. If I've not explained something very well then just holla.
__________________
There are no computers in heaven!
Reply With Quote
  #5  
Old 03-19-2012, 10:53 AM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default How do I?

What is the Initialisation routine?
How do i declare answer in the Button.Click?

Last edited by EMetalmulishaH; 03-19-2012 at 11:00 AM. Reason: Adding Something
Reply With Quote
  #6  
Old 03-19-2012, 11:00 AM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Initialisation is just a word.

I use it to describe stuff that has to be set up for the program to run. So, in your example, the things that need to be initialised are...

1. Num1 randomised.
2. Num2 randomised.
3. numLabel.Text set to the question.
4. Cnt set to zero.

So, you need to put those 4 things into a routine. You can call the routine anything, but being descriptive helps so it's worth calling it Initialise so you know what it's doing, and it's simple to call.
Code:
' Pseudo codeish
Private Sub Initialise
    Num1 randomised.
    Num2 randomised.
    numLabel.Text set to the question.
    Cnt set to zero.
End Sub
You can then call that routine whenever you need to initialise (i.e. Form Load, correct answer, 3 incorrect answers) and be sure everything is set up correctly for the next question.
__________________
There are no computers in heaven!
Reply With Quote
  #7  
Old 03-19-2012, 11:05 AM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

Okay Thank You.
Reply With Quote
  #8  
Old 03-19-2012, 11:52 AM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

I am having Issues in Running the Initialize Routine After I get the answer correct.
And I can't get the label to show up since i moved it in the Initialize box.

Public Class Form1
Dim num1Random As New Random()
Dim num1 As Integer = num1Random.Next(1, 11)
Dim num2 As Integer = num1Random.Next(1, 11)
Dim answer As Integer
Dim Cnt As Integer = 0

Private Sub Initialise(ByVal num1 As Integer, ByVal num2 As Integer)
numLabel.Text = numLabel.Text = ("What is " & num1 & " times " & num2 & " ?")
Randomize(num1)
Randomize(num2)
Cnt = 0
End Sub

Public Sub numButton1_Click(sender As System.Object, e As System.EventArgs) Handles numButton1.Click

answer = Val(TextBox1.Text)

If answer = (num1 * num2) Then
MessageBox.Show("Correct! Time for another question")


Else
MessageBox.Show("INCORRECT! Try Again.")
Cnt = Cnt + 1

If Cnt < 3 Then
MessageBox.Show("INCORRECT! Try Again.")

If Cnt = 3 Then
MessageBox.Show("INCORRECT! Correct answer was: " & (num1 * num2) & ". Time for another question.")

Else
MessageBox.Show("Correct! Time for another question")


End If
Exit Sub
End If
End If
End Sub
End Class
Reply With Quote
  #9  
Old 03-19-2012, 12:00 PM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

I am glad I found this site, wish I could of found it Earlier though.
Reply With Quote
  #10  
Old 03-19-2012, 12:05 PM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

You've got to think about the order of stuff.

You want to randomize the numbers before putting the numbers into a message to display in the label.

What you've got going on is the form loads and randomizes the numbers because you randomize them in the declaration. You're then putting those numbers into the label, then randomising them into new numbers. So when it comes to calculating the result, the numbers it is calculating with are NOT the numbers that were put in the label.

In your button click...

If the answer is correct then tell them they have got it correct, then initialise the next question.

If they get the question wrong then increment how many times they have got it wrong. If they have got it wrong 3 times then tell them the answer and initialise the next question, else tell them they are incorrect.

In pseudo code, what I have described above would be...
Code:
Sub Click

    If Correct Then
        Tell the user they are correct
        Initialise the next question
    Else
        Increment wrong count

        If wrong 3 times then
            Tell them the correct answer
            Initialise the next question
        Else
            Tell them they are wrong
        End if
    End if
End Sub
From the code you have posted you aren't calling the initialisation routine in the form_load event.
__________________
There are no computers in heaven!
Reply With Quote
  #11  
Old 03-19-2012, 12:12 PM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Your location says Mississippi.

I'd suggest spelling Initialize the American way if that's the case, just in case your teacher is on the ball.

I'm UK based, hence me spelling it the UK way.
__________________
There are no computers in heaven!
Reply With Quote
  #12  
Old 03-19-2012, 12:19 PM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

How would I set num1 and num2 in the Initial Routine to only numbers (1-10)
If i delete the num1, num2 = num1Random.next(1, 11)
I have Randomize(num1)
Randomize (num2)
Reply With Quote
  #13  
Old 03-19-2012, 12:21 PM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

My teacher is pretty cool, he just doesn't teach us all the things we need to know.
Reply With Quote
  #14  
Old 03-19-2012, 12:22 PM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

You already know how to do it.

You declare the variables as...
Code:
Dim num1 As Integer = num1Random.Next(1, 11)
 Dim num2 As Integer = num1Random.Next(1, 11)
I think that should really be Next(1, 10) if you want between 1 and 10.

Because the variables are declared, you don't need to declare them in the initialisation routine, so it just becomes...
Code:
num1 = num1Random.Next(1, 10)
num2 = num1Random.Next(1, 10)
__________________
There are no computers in heaven!
Reply With Quote
  #15  
Old 03-19-2012, 12:23 PM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

Can we go over little bit at a time?
And thank you so much for helping me out, I know I am probably frustrating You.

Public Class Form1
Dim num1Random As New Random()
Dim num1 As Integer = num1Random.Next(1, 11)
Dim num2 As Integer = num1Random.Next(1, 11)
Dim answer As Integer
Dim Cnt As Integer

Private Sub Initialise()
Randomize(num1)
Randomize(num2)
numLabel.Text = numLabel.Text = ("What is " & num1 & " times " & num2 & " ?")
Cnt = 0

End Sub
Reply With Quote
  #16  
Old 03-19-2012, 12:24 PM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default Oh

So i just move them down okay, I understand.
Reply With Quote
  #17  
Old 03-19-2012, 12:29 PM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

Public Class Form1
Dim num1Random As New Random()
Dim num1 As Integer = num1Random.Next(1, 11)
Dim num2 As Integer = num1Random.Next(1, 11)
Dim answer As Integer
Dim Cnt As Integer

Private Sub Initialise()
num1 = num1Random.Next(1, 11)
num2 = num1Random.Next(1, 11)
numLabel.Text = numLabel.Text = ("What is " & num1 & " times " & num2 & " ?")
Cnt = 0

End Sub
Reply With Quote
  #18  
Old 03-19-2012, 12:32 PM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Quote:
Originally Posted by EMetalmulishaH View Post
So i just move them down okay, I understand.
Not so much move them down.

You still need to declare the variables with Form scope because the variables are shared across routines and each routine needs to know about the variable. So having it form scope allows one routine to set them up (i.e. Initialise) and another routine to see what the were set to (i.e. the Click event that looks at the calculation).

But originally you had the program doing one question OK but then got stuck. One of the things you needed to do for a new question was to create new random numbers. So having the randomize with the variable declaration wasn't any use any more. That only got called when the form was created. You need to be able to call the randomize again, hence moving that to the initialisation routine.

So the declaration doesn't move (i.e. dim Num1 as integer), but the Randomize part does move.
__________________
There are no computers in heaven!
Reply With Quote
  #19  
Old 03-19-2012, 12:42 PM
EMetalmulishaH's Avatar
EMetalmulishaH EMetalmulishaH is offline
Freshman
 
Join Date: Mar 2012
Location: NO WHERE
Posts: 39
Default

On the numLabel.text, do i keep it in its on box at the bottom?

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
numLabel.Text = numLabel.Text = ("What is " & num1 & " times " & num2 & " ?")
End Sub


Or do I move that in the Initialize and delete the bottom One?
Reply With Quote
  #20  
Old 03-19-2012, 12:54 PM
DrPunk's Avatar
DrPunkMultiplication Program DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I'm not entirely clear what it is you're asking.

The Form_Load event will only ever fire once. When the form loads.

So that's fine for setting up the first question, but the point here is that you'll need to set up a question multiple times. Not just when the form loads.

There's 4 things that you do to initialise a question. We've already sorted them out.

So you write those four things when the form loads. You could write those 4 things again when they get the question right. And you could write those 4 things when they get the question wrong 3 times.

But those 4 things are the same for the form loading, them answering correctly and them answering incorrectly 3 times. So rather than writing those 4 things out 3 times it's easier to write them once in a single routine and call that routine 3 times. It also means that when you want to initialise a new question that you don't accidentally forget a step which would cause the program to go wrong.
__________________
There are no computers in heaven!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Multiplication Program
Multiplication Program
Multiplication Program Multiplication Program
Multiplication Program
Multiplication Program
Multiplication Program Multiplication Program Multiplication Program Multiplication Program Multiplication Program Multiplication Program Multiplication Program
Multiplication Program
Multiplication Program
 
Multiplication Program
Multiplication Program
 
-->