Hello I need help on an assignment (loops)

moneyman1188

New member
Joined
Mar 9, 2006
Messages
2
Programming Experience
Beginner
Hello I'm a highschool student taking a VB.net class our school offers and I was given a task today that boggled my mind in all different directions. Well here it is maybe you could help me out a bit. This program can only use semi-basic looping. I can't have a program that is way ahead of our class.

Develop a progeam that will use an input box to query the user for a positive number. Have the display, in two seperate text boxes. The number of * in the longest row in each case is equal to the nubmer input by the user. (4 was used in the example below)

Text box #1
*
**
***
****

Text box #2
****
***
**
*
 
So you haven't got a clue? Here is one suggestion:
VB.NET:
Dim input As String = InputBox("input number:")
If IsNumeric(input) = True Then
  Dim inputNum As Short = CShort(input)
  Dim str1, str2 As String
  For i As Short = 1 To inputNum
    str1 += New String("*", i) + vbNewLine
    str2 += New String("*", inputNum - i + 1) + vbNewLine
  Next
  MsgBox(str1 + vbNewLine + str2)
End If
You can put str1 and str2 in two multiline textboxes if you like.
Hope I didn't ruin your learning process.. :rolleyes:
 
Ok I used your code to guide me, thanks alot for it :)

So far I got this:

VB.NET:
 Dim x As Integer
        Dim y As Integer
        Dim input As Integer
        Dim counter As Integer
        Dim str1, str2 As String
        Dim z As Integer
        
        input = InputBox("Please type in a positive whole number:")
        counter = input

        For x = 1 To input
            str1 = str1 & "*" & vbNewLine
            For y = 1 To counter
                str2 = str2 & "*"
            Next y
            str2 = str2 & vbNewLine
            counter = counter - 1
        Next x

        txtOne.Text = str1
        txtTwo.Text = str2


The second text box works perfect but the first textbox is giving my trouble... If you excute this code it gives you this (if 3 was entered):

Textbox #1
*
*
*

Textbox #2
***
**
*

I want the first textbox to look like this:
*
**
***


If you could help me fix my code up I'd appreciate it :)
 
You need to put a loop around updating your str1 variable similar to the way you did for your str2. Otherwise you just add one "*" and a vbNewLine every time you go through the big loop. Add the vbNewLine to str1 after you exit loop. (Just as you did for str2).

Hint: For y = x To 1 Step -1
 
moneyman1188, you're not allowed to use the String constructor? How strange aren't the schools today.
VB.NET:
For x = 1 To input
  For y = 1 To input - counter + 1
    str1 = str1 & "*"
  Next y
  str1 += vbNewLine
  For y = 1 To counter
    str2 = str2 & "*"
  Next y
  str2 += vbNewLine
  counter -= 1
Next x
TrtnJohn, the y variable value isn't used here, so it doesn't matter if you step forward or backward, it's the number of steps that matter here. They probably aren't allowed to step backward yet also.. :)
 
How strange, i had a VERY similar task set the other day for me too,
except i had to do ::


*
***
*****
*******
*****
***
*

Shame I forgot to save the project to my usb drive.

From what I remember it was something like:


VB.NET:
        For a = 1 To 8 Step 2

            For b = 1 To a
                strDiamond = strDiamond & "*"
            Next

            strDiamond = strDiamond & vbNewLine

        Next
        For a = 6 To 1 Step -2

            For b = 1 To a - 1
                strDiamond = strDiamond & "*"
            Next

            strDiamond = strDiamond & vbNewLine

        Next
        TbStr.Text = strDiamond
 
Last edited:
Back
Top