Hey there guys,

First time poster here, so I apologize for being a rookie. This is my first VB class and im having a hard time with For-Next loops.

The question assigned is as follows:

Request and odd number, and display a triangle similar to this:

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

with the input number of stars in the front row.

My code so far is as follows:

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim numStars As Integer
        lstOutput.Items.Clear()
        numStars = CInt(InputBox("Enter amount of stars: (Odd numbers only)"))
        If numStars Mod 2 = 0 Then
            MsgBox("Not an odd number. Please try again.", , Nothing)
        Else
            ShowBox(numStars)
        End If
    End Sub


    Sub ShowBox(ByVal numStars As Integer)
        Dim row As String = " "
        For i As Integer = numStars To 1 Step -1
            row &= "*"
            lstOutput.Items.Add(row)
        Next
    End Sub

As you can see it is only an issue of having the maximum number of stars on top, rather than at the bottom. I figure the issue is just reworking the loop, or adding another nested loop, but I am having a hard time finding the solution.

Hey there guys,

First time poster here, so I apologize for being a rookie.

Then you should have been posting for the past year. Then you wouldn't be a rookie :icon_mrgreen:

This is my first VB class and im having a hard time with For-Next loops.

As you can see it is only an issue of having the maximum number of stars on top, rather than at the bottom. I figure the issue is just reworking the loop, or adding another nested loop, but I am having a hard time finding the solution.

You aren't having a hard time. you're second-guessing yourself. You already know the answer (bolded above).

You need one loop for each line. Within that loop you need another loop for each '*' on that line.

Thanks a lot Walt! I added another loop and it worked for me. Here is my solution:

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'Assign variables
        Dim numStars, topSize As Integer
        'clear listbox
        lstOutput.Items.Clear()
        'Cast input as integer
        numStars = CInt(InputBox("Enter amount of stars: (Odd numbers only)"))
        topSize = numStars
        'Find odd number
        If numStars Mod 2 = 0 Then
            'not an odd number
            MsgBox("Not an odd number. Please try again.", , Nothing)
        Else
            'odd number
            For numStars = topSize To 1 Step -1
                ShowBox(numStars)
            Next
        End If
    End Sub


    Sub ShowBox(ByVal numStars As Integer)
        'assign variable
        Dim row As String = " "
        'Make triangle
        For i As Integer = 1 To numStars
            row &= "*"
        Next
        lstOutput.Items.Add(row)
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.