Public Class Form1
Dim x As Integer = 1
Dim y As Integer = 1
Dim z As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Do
z = ""
Do
z = z & (y * x) & " "
y = y + 1
Loop While (y <= 10)
x = x + 1
lstBox.Items.Add(z)
Loop While (x <= 10)
End Sub
End Class
9 Replies - 276 Views - Last Post: 21 January 2013 - 01:46 AM
#1
Need to know what did I do wrong in this VB code....
Posted 16 January 2013 - 06:37 AM
Replies To: Need to know what did I do wrong in this VB code....
#2
Re: Need to know what did I do wrong in this VB code....
Posted 16 January 2013 - 06:38 AM
errors?
#3
Re: Need to know what did I do wrong in this VB code....
Posted 16 January 2013 - 06:39 AM
yep...I'm doing some multiplication table...
#4
Re: Need to know what did I do wrong in this VB code....
Posted 16 January 2013 - 08:10 AM
Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.
I'm betting resetting it back to an initial value somewhere will fix your issue.
I'm betting resetting it back to an initial value somewhere will fix your issue.
This post has been edited by CharlieMay: 16 January 2013 - 08:13 AM
#5
Re: Need to know what did I do wrong in this VB code....
Posted 16 January 2013 - 03:00 PM
CharlieMay, on 16 January 2013 - 08:10 AM, said:
Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.
I'm betting resetting it back to an initial value somewhere will fix your issue.
I'm betting resetting it back to an initial value somewhere will fix your issue.
Well I'm on the right to Y...Because I need it to go up to 10...what I did wrong was the x part...it won't go down with 2,3,4, and so on...also the 2nd row of numbers together with the others are not showing the numbers right...that's why I'm asking what did I do wrong with the code...
#6
Re: Need to know what did I do wrong in this VB code....
Posted 16 January 2013 - 03:49 PM
talahib098, on 16 January 2013 - 04:00 PM, said:
CharlieMay, on 16 January 2013 - 08:10 AM, said:
Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.
I'm betting resetting it back to an initial value somewhere will fix your issue.
I'm betting resetting it back to an initial value somewhere will fix your issue.
Well I'm on the right to Y...Because I need it to go up to 10...what I did wrong was the x part...it won't go down with 2,3,4, and so on...also the 2nd row of numbers together with the others are not showing the numbers right...that's why I'm asking what did I do wrong with the code...
Did you even look at your code while thinking about what CharlieMay said?
After your first time through the inner Do loop, y has a value 0f 11. You then put z into the ListBox, and go back to generate the next line. y STILL has a value of 11, and you multiply it by x, which has a value of 2, giving you 22. You then drop out of the loop because y is greater than 10.
So no, you do NOT have y right.
You would have found this problem in about 30 seconds if you had set a breakpoint and single-stepped through the code. For information on how to do that, see our tutorial on Debugging Skills.
#7
Re: Need to know what did I do wrong in this VB code....
Posted 16 January 2013 - 04:10 PM
lar3ry, on 16 January 2013 - 03:49 PM, said:
talahib098, on 16 January 2013 - 04:00 PM, said:
CharlieMay, on 16 January 2013 - 08:10 AM, said:
Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.
I'm betting resetting it back to an initial value somewhere will fix your issue.
I'm betting resetting it back to an initial value somewhere will fix your issue.
Well I'm on the right to Y...Because I need it to go up to 10...what I did wrong was the x part...it won't go down with 2,3,4, and so on...also the 2nd row of numbers together with the others are not showing the numbers right...that's why I'm asking what did I do wrong with the code...
Did you even look at your code while thinking about what CharlieMay said?
After your first time through the inner Do loop, y has a value 0f 11. You then put z into the ListBox, and go back to generate the next line. y STILL has a value of 11, and you multiply it by x, which has a value of 2, giving you 22. You then drop out of the loop because y is greater than 10.
So no, you do NOT have y right.
You would have found this problem in about 30 seconds if you had set a breakpoint and single-stepped through the code. For information on how to do that, see our tutorial on Debugging Skills.
in saying that...I did thought that I was really wrong about y there...sorry didn't understand what CharlieMay said...
#8
Re: Need to know what did I do wrong in this VB code....
Posted 17 January 2013 - 07:04 AM
Here's what I see your loops do.
Mind you, I am far from an expert.
If you don't reset Y somewhere as instructed, Y will continue to increment past 10 until the outer loop finishes (X <= 10)
Mind you, I am far from an expert.
'outer loop
Do
z = ""
' sets Z, then start inner loop.
'inner loop
do
instructions
Loop while y <= 10
'end inner loop
' inner loop processes itself until Y is less than or equal to 10
' then it jumps to the next instructions in the outer loop.
x = x + 1
listbox.add(Stuff)
While x <= 10
' so we add one to X, check to see if X is less than or equal to 10 and start over. Y STILL equals ten, so the inner loop doesn't loop but does process the instruction once since your do loop checks AFTER the instructions are done. But now Y = 11
'end outer loop
If you don't reset Y somewhere as instructed, Y will continue to increment past 10 until the outer loop finishes (X <= 10)
#9
Re: Need to know what did I do wrong in this VB code....
Posted 17 January 2013 - 09:22 AM
#10
Re: Need to know what did I do wrong in this VB code....
Posted 21 January 2013 - 01:46 AM
Public Class Form1
Dim x As Integer
Dim y As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
x = 1
Do
y = 1
Do
lstBox.Items.Add(x * y)
y = y + 1
Loop While (y <= 10)
x = x + 1
Loop While (x <= 10)
End Sub
End Class
well that's the answer, I got it just a moment ago...where we will gonna pass it to my prof...tnx for the advice and hints
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|