# Math Problems!

• (2 Pages)
• 1
• 2

## 21 Replies - 5521 Views - Last Post: 04 October 2011 - 10:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=246412&amp;s=273cceed1819e262434bca396ac8216b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

# Math Problems!

Posted 08 September 2011 - 02:44 AM

```Public Class frmSums
Dim Ran As New Random
Dim Int As New Integer
Dim Num1 As New Integer
Dim Num2 As New Integer

Private Sub frmSums_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Application.Exit()

End Sub

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Me.Visible = False

Randomize()
Num1 = Ran.Next(1, 5000)
Num2 = Ran.Next(1, 5000)
Label1.Text = (Num2) & vbCrLf & " + " & vbCrLf & (Num1)
Num1 = Ran.Next(1, 5000)
Num2 = Ran.Next(1, 5000)
Label2.Text = (Num2) & vbCrLf & " + " & vbCrLf & (Num1)
End Sub

End Class
```

I am trying to place random numbers into labels, but the process that i am doing is taking way to long, just wondering if there is an easy way around this? I also need to know the answer to the sums that get placed into the labels and then check that against the answer in the text box.

I have looked all over the DIC forums and nothing meets my queries.
Please help, this is just a quick program for my son so that he can get some sums into him. Thanks

Is This A Good Question/Topic? 0

## Replies To: Math Problems!

### #2 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Math Problems!

Posted 08 September 2011 - 03:30 AM

So you have numbers in labels then you can have its sum: But your two numbers are num1 and num2, so:
```Dim sum As Integer = num1 + num2
```

Then check if sum = val(textBox.Text)

### #3 demausdauth

Reputation: 190
• Posts: 689
• Joined: 03-February 10

## Re: Math Problems!

Posted 08 September 2011 - 06:06 AM

If you are trying to just show the addition problems and not let the user enter an answer to check then he is a simplified version.

```
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Me.Visible = False

Randomize()
Num1 = Ran.Next(1, 5000)
Num2 = Ran.Next(1, 5000)
Label1.Text = String.Format("{0} + {1} = {2}", Num1,Num2,(Num1 + Num2))
Num1 = Ran.Next(1, 5000)
Num2 = Ran.Next(1, 5000)
Label2.Text = String.Format("{0} + {1} = {2}", Num1,Num2,(Num1 + Num2))
End Sub

```

### #4 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Math Problems!

Posted 08 September 2011 - 06:23 AM

Then you have displayed the answer before, what is doing there is asking someone to give he answer and then check if it is correct or not

### #5 demausdauth

Reputation: 190
• Posts: 689
• Joined: 03-February 10

## Re: Math Problems!

Posted 08 September 2011 - 06:56 AM

The OP does not state he is waiting for the user to answer before displaying the sum to the math problem. In fact he states:

Quote

Please help, this is just a quick program for my son so that he can get some sums into him.

To me this meant, he just want to flash the problem on the screen with the answer so that the child could understand math.

### #6 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

## Re: Math Problems!

Posted 08 September 2011 - 06:36 PM

sorry for the misunderstanding, This is the go.
There are 20 labels on screen eace with new sums in them. IE:
```Randomize()
Num1 = Ran.Next(1, 5000)
Num2 = Ran.Next(1, 5000)
Label1.Text = (Num2) & vbCrLf & " + " & vbCrLf & (Num1)
```

under each label is a textbox which my son needs to enter in the answer that he suspects is correct. If the answeris correct then messagebox.show("Great Job, Answer Is Correct") else messagebox.show("Try Again, Answer Is Incorrect").

I hope this expleains it a little better. cheers.

### #7 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

## Re: Math Problems!

Posted 11 September 2011 - 07:59 PM

So I am going back over and just doing one Label now with 1 sum in it. problem is at Line 14 there is an error saying "Use the 'new' keywork to creaete an object instance" ... WTF does that mean? this same code worked on my other form and i am unsure as to why this does not work!

```Option Explicit On
Public Class Form1

Dim Correct As Integer
Dim Wrong As Integer
Dim Ran As Random
Dim Num1 As New Integer
Dim Num2 As New Integer

Private Sub btnSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSum.Click

Randomize()
Num1 = Ran.Next(1, 1000)
Num2 = Ran.Next(1, 1000)
lblSum.Text = (Num1) & vbCrLf & "+" & vbCrLf & (Num2)
Dim Ans As Integer = Num1 + Num2
txtRight.Text = Correct + 1
Else
txtWrong.Text = Wrong + 1
End If

Randomize()
Num1 = Ran.Next(1, 1000)
Num2 = Ran.Next(1, 1000)
lblSum.Text = (Num1) & vbCrLf & "--" & vbCrLf & (Num2)
Dim Ans As Integer = Num1 - Num2
txtRight.Text = Correct + 1
Else
txtWrong.Text = Wrong + 1
End If
Randomize()
Num1 = Ran.Next(1, 1000)
Num2 = Ran.Next(1, 1000)
lblSum.Text = (Num1) & vbCrLf & "/" & vbCrLf & (Num2)
Dim Ans As Integer = Num1 / Num2
txtRight.Text = Correct + 1
Else
txtWrong.Text = Wrong + 1
End If
Randomize()
Num1 = Ran.Next(1, 1000)
Num2 = Ran.Next(1, 1000)
lblSum.Text = (Num1) & vbCrLf & "x" & vbCrLf & (Num2)
Dim Ans As Integer = Num1 * Num2
txtRight.Text = Correct + 1
Else
txtWrong.Text = Wrong + 1
End If
End If

End Sub
End Class

```

Here is a picture of the Form.

### #8 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

## Re: Math Problems!

Posted 11 September 2011 - 09:11 PM

geeze, now i feel really dumb, i forgot to a NEW to the random process eg:
```Dim Ran as Random.
'Should've been'
Dim Ran As New Random
```

Thanks anyways

### #9 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

## Re: Math Problems!

Posted 11 September 2011 - 09:29 PM

And yet again here I go. So i have the Math randomness part down pat, but now everytime the user gets the answer correct the txtRight.text changes to 1, Now if i were to do another sum then txtRight.Text does not change from 1. how do i step it up in intervals?

```Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click

Dim Correct As Integer
Dim Wrong As Integer
txtRight.Text = Correct + 1
Else
txtWrong.Text = Wrong + 1
End If
End Sub
```

### #10 demausdauth

Reputation: 190
• Posts: 689
• Joined: 03-February 10

## Re: Math Problems!

Posted 12 September 2011 - 06:41 AM

You will need to use either Class level variables (as you had Correct and Wrong in post #7) to track your correct and incorrect answers or you will need to use the value from txtRight and txtWrong and add to those values:

Class level variables
```Public Class Form1
Dim Correct As Integer
Dim Wrong As Integer

Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click

txtRight.Text = (Correct + 1).ToString()
Else
txtWrong.Text = (Wrong + 1).ToString()
End If
End Sub
End Class

```

From the text values
```     Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click

If Not String.IsNullOrEmpty(txtRight.Text.Trim())
txtRight.Text = (CInt(txtRight.Text) + 1).ToString()
Else
txtRight.Text = 1
End If
Else
If Not String.IsNullOrEmpty(txtWrong.Text.Trim())
txtWrong.Text = (CInt(txtWrong.Text)+1).ToString()
Else
txtWrong.Text = 1
End If
End If
End Sub

```

### #11 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

## Re: Math Problems!

Posted 12 September 2011 - 04:22 PM

not to sound ungrateful demausdauth, but there is/was an easier way. which i figured out myself but forgot to post it.
here tis.

```correct = correct + 1
txtRight.text = correct

```

cheers anyways.

### #12 demausdauth

Reputation: 190
• Posts: 689
• Joined: 03-February 10

## Re: Math Problems!

Posted 13 September 2011 - 05:51 AM

My bad. I think in C#-ish and write in VB sometimes.

What I had meant for the class level variables was essentially what you last posted.

```Public Class Form1
Dim Correct As Integer
Dim Wrong As Integer

Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click

Correct += 1
txtRight.Text = Correct.ToString()
Else
Wrong += 1
txtWrong.Text = Wrong.ToString()
End If
End Sub
End Class

```

Also something to point out, while VB will allow you assign the variable correct (integer type) to the Text property (string type), imo, you do the cast before assigning. And in this case it is as simple as .ToString(). Makes it easier later when debugging problems to know what the type of the variable is.

### #13 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

## Re: Math Problems!

Posted 03 October 2011 - 11:03 PM

bit of a problem with this code. the problem is that i am trying to divide the 2 numbers. this works greatbut there is a problem eg:

45 / 9 = 5 < correct answer works good here.
30 / 9 = 3 < there is .3333 remainding. dont want to show that, just want to see 3. works good.
35 / 9 = 3 < there is .888 remaining. dont want to show that HOWEVER the answer that the program pumps out as correct is 4. as it is rounding up. i need this to only show that 9 goes into 35, 3 times only. if you put the answer as 3 its wrong. what is the way to fix this? im stumped!

```Randomize()
Num1 = Ran.Next(1, 51)
Num2 = Ran.Next(1, 11)
lblSum.Text = (Num1) & vbCrLf & "/" & vbCrLf & (Num2)
Ans = Num1 / Num2
```

thanks all

### #14 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Math Problems!

Posted 03 October 2011 - 11:08 PM

You can use Math.Floor(ans) and it will truncate all numbers after decimal

### #15 PNJLj

Reputation: 6
• Posts: 231
• Joined: 18-May 09

## Re: Math Problems!

Posted 03 October 2011 - 11:25 PM

nah not working. tried it in all areas but still no go.
i don't actually have decimal numbers. the thing i want is to just put in the single answer.

ie: 36/9 = 4 .... 37/9 = 4 .... 35/9 = 3