My first VB console application (HELP!)

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
Ok so I'm doing one of those "Teach yourself VB" type books. And at the end there are practice exams and practice programs to write. The web is the only place I have to ask questions or check my code..etc. Well so far things are going great and I was feeling fairly confident. Now after reading my last chapter I ran into another practice program to write. The chapter was entirely on console applications. However none of the reading showed me how to go about such a program. Trust me, books are hard. I look at source all day and it truly helps but reading then doing is sometimes difficult. I've never done a console app in VB and I'm truly stuck on how to do this.

VB.NET:
Write a Console Application that uses nested for loops to draw a diamond pattern of asterisks (see below).  
Have your program prompt the user for any odd number and then displays the corresponding diamond shape as shown below:
Example: If the user inputs 3:

 *
***
 *

Example: If the user inputs 5:

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

So the user can enter ANY odd number. I know I'm going to have to figure out how to check if the number is odd. And then figure out how to do a loop to add 2 to each asterisk and then subtract 2 as well. I also know I have to figure out the spacing for each one so the asterisk starts in the right place as well. I have no idea how to tackle this.
 
Tricky star formation code

Admittedly, this is a very tricky problem, but here is my solution. See if you can use the general idea to come up with some creative star formations of your own:

VB.NET:
Module Module1

    Sub Main()
        Dim snum As String, num, s As Integer
        Console.Write("Enter an odd number up to 23:  ")
        snum = Console.ReadLine
        Integer.TryParse(snum, num)
        If num Mod 2 = 0 Then num += 1 'verify odd number
        If num > 23 Then num = 23 'restrict size to fit on screen

        For x As Integer = 1 To num Step 2
            s = (num - x) \ 2
            Console.Write(Space(s))
            For y As Integer = 1 To x
                Console.Write("*")
            Next y
            Console.WriteLine()
        Next x

        For x As Integer = num - 2 To 1 Step -2
            s = (num - x) \ 2
            Console.Write(Space(s))
            For y As Integer = 1 To x
                Console.Write("*")
            Next y
            Console.WriteLine()
        Next x
        Console.ReadLine()
    End Sub

End Module

If you need explanation of any part of the code, then post your questions.

Out of curiosity, what is the name of your book? Please state the title, author, publisher, date, and ISBN # if you have it.

(PS: I teach VB and use mostly console applications in order to focus on logic and structure rather than on the GUI design.)
 
Last edited:
Another solution here, using same calculation but managing lines with a string list:
VB.NET:
Sub Main()
    Console.Write("input an odd number: ")
    Dim input As String = Console.ReadLine
    Dim number As Integer
    If Integer.TryParse(input, number) Then
        If number Mod 2 <> 0 Then
            Dim lines As New List(Of String)
            lines.Add(New String("*"c, number))
            For x As Integer = number - 2 To 1 Step -2
                Dim spaces As New String(" "c, (number - x) \ 2)
                Dim stars As New String("*"c, x)
                lines.Insert(0, spaces & stars)
                lines.Add(spaces & stars)
            Next
            Console.WriteLine(String.Join(vbNewLine, lines.ToArray))
        Else
            Console.WriteLine("that was not an odd number.")
        End If
    Else
        Console.WriteLine("unable to interpret numeric input.")
    End If
    Console.Write("press any key to exit.")
    Console.ReadKey(True)
End Sub
You can substitute the "x stars" line with another For loop to satisfy the given task of using "nested loops" but it's not necessary for output.
 
Back
Top