|
-
Oct 30th, 2002, 03:34 PM
#1
Thread Starter
New Member
Extremely simple looping Q
I'm JUST starting to learn VB and JUST starting to learn looping. Anyways I have a little program I need to make using simple looping (probably laughable to you) and I'm really confused . I havn't understood the written explanations (codeless) so I'm hoping someone could provide some code for a begginner. Here it is
Create a program that will print the integers between 1 and 100 according to the descriptions
a) Print them on the sceen in ten rows of ten digits.
b)Print only the even numbers between 1 and 100 in five rows of 10 digits
c)Print only every fifth number between 1 and 100 in four rows of five digits
Any help would be greatly appreciated, btw it's in VB6
-
Oct 30th, 2002, 03:50 PM
#2
Addicted Member
This is untested so it may not work properly. First draw a label on your form. Then in the Form_Load event type:
VB Code:
Dim i, count as Integer
For i = 1 to 100
count = count + 1
If count > 10 Then
count = 0
Label1.Caption = Label1.Caption + vbCrLf
End If
Label1.Caption = Label1.Caption + i
Next
That should get you on your way.
-
Oct 30th, 2002, 04:09 PM
#3
Addicted Member
Here's something to get you started:
VB Code:
Dim X As Long
' Ten rows of ten.
For X = 1 To 100
Me.Print CStr(X)
If X Mod 10 = 0 Then
Me.CurrentX = X * 10
Me.CurrentY = 0
End If
Next
' Five rows of ten, evens only.
For X = 2 To 100 Step 2
Me.Print CStr(X)
If X Mod 20 = 0 Then
Me.CurrentX = X * 10
Me.CurrentY = 0
End If
Next
I'll let you figure out that last condition. Also, this is all off the top of my head, so test it out first.
-
Oct 31st, 2002, 07:02 AM
#4
Thread Starter
New Member
Thanks
Thanks guys this should help get me started
-
Oct 31st, 2002, 07:11 AM
#5
Addicted Member
Originally posted by run_GMoney
This is untested so it may not work properly. First draw a label on your form. Then in the Form_Load event type:
That should get you on your way.
Does that make both "i" and "count" an integer?
-
Oct 31st, 2002, 08:31 AM
#6
Fanatic Member
no, only count, you need to do each one, like so:
VB Code:
dim i as string, k as string, b as integer, LgD as long, oD as byte
or this is kool, you can do all variables from lets say, A to D, as Integer
VB Code:
DefInt A-D
DefStr A-Z '(makes all var's starting with A to Z strings, I think)
-
Oct 31st, 2002, 03:34 PM
#7
Thread Starter
New Member
Once again thanks for the help but I've come across another one I can't seem to figure out. Using looping (I'm pretty sure a for..next loop or a do...loop) I have to ask the user for a number between 1 and 20 then use that number as the value to create the following patterns
a) make the pattern - e.g the user entered 5
*
**
***
****
*****
b) Make this pattern e.g the user entered 4
* *
** **
*** ***
********
c) Make this pattern e.g the user entered 3
*
**
***
**
*
uggh! I give up. The last 2 shouldn't look like that . b)should look like a crown and c) should look like 2 triangles put together at the base.
I've tried all three but with no luck.
Last edited by Hotstreak; Oct 31st, 2002 at 03:55 PM.
-
Oct 31st, 2002, 04:16 PM
#8
Addicted Member
Let's keep this simple.
Here is an example of number 3.
The code is very basic, to help you understand.
Put a Label on your form, and set multiline to true.
Put this code in the Form Load event:
VB Code:
Dim sNum As Long
Dim sText As String
Dim sAsters As String
Dim i As Long
' string of 20 asterisks
sAsters = "********************"
' ask for a number
sNum = InputBox("Enter a number", "Input")
' loop up to form the first part of the triangle
For i = 1 To sNum
sText = sText & Left(sAsters, i) & vbNewLine
Next i
' take 1 off the imput number and step down to create the other triangle
sNum = sNum - 1
For i = sNum To 1 Step -1
sText = sText & Left(sAsters, i) & vbNewLine
Next i
Label1.Caption = sText
Now I know that there are better ways to do this but hopefully it demonstrates to you how to get started.
Obviously Number 1 can be acheived with just the first Loop.
If you want me to do 2 let me know...
Some days you're the dog,
and some days you're the hydrant.
VB6 Enterprise
-
Oct 31st, 2002, 04:55 PM
#9
Thread Starter
New Member
Ignore all (((((((
Thanks! The simpler it is right now, the better but hmm. . . I tried to get number 3
to () *
look **
like ***
this **
And *
2
look * ) )))))) *
like) ** )))) **
this *** ) ***
but ********
in my last post but couldn't get the **'s to line up right(I don't know if the code you provided was set up to display it like that)
Also if you could show me #2 thatwould help me a lot.
IGNORE all the ))() that's the only way I could get the **'s to set up right
-
Oct 31st, 2002, 05:02 PM
#10
Addicted Member
Ok, I get the idea. I will do those examples for you.
Some days you're the dog,
and some days you're the hydrant.
VB6 Enterprise
-
Oct 31st, 2002, 07:56 PM
#11
Addicted Member
Finally...
This really had me stumped. I could not get the crown to display properly...for some reason I kept getting an extra space in the first string....anyway...here is the code.
For this to work, make the Label Font Courier New. The default MS Sans Serif font is (I think) variable pitch and won't display properly. Courier New is fixed pitch and does the job.
This example should be enough for you to figure out the last example on your own.
VB Code:
Dim sNum As Long
Dim sText As String
Dim sAsters As String
Dim lSpc As Long
Dim lTotalSpc As Long
Dim i As Long
' string of 20 asterisks
sAsters = "********************"
sText = ""
' ask for a number
sNum = InputBox("Enter a number", "Input")
lTotalSpc = sNum * 2
Label1.Caption = ""
For i = 1 To sNum
lSpc = lTotalSpc - (i * 2)
sText = Left(sAsters, i) & Space(lSpc) & Left(sAsters, i)
Label1.Caption = Label1.Caption & sText & vbNewLine
Next i
Good Luck and let me know if you need any more help.
Some days you're the dog,
and some days you're the hydrant.
VB6 Enterprise
-
Oct 31st, 2002, 08:31 PM
#12
Thread Starter
New Member
Thanks
It sounds like you went to a bit of trouble just for me!
Thanks a lot , I probably can't try it out until tommorow but then I'll tell you how it worked out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|