locked
Display Asterisk for user input RRS feed

  • 問題

  • I'm writing a Visual Basic program about display asterisk. The user can input any positive number in the console screen.

    When user input positive number 6, the console screen will show like this:

    ******

     *****

      ****

       ***

        **

         *

    When user input positive number 9, the console screen will show like this:

    *********

     ********

      *******

       ******

        *****

         ****

          ***

           **

            *

     

     

    My VB code is like this:

    Module Module1

        Dim number As Integer
        Dim i As Integer
        Dim j As Integer

        Dim x As String = "*"

        Sub Main()

            Console.WriteLine("Please input a number")

            number = Console.ReadLine()

            Compute()

            Console.WriteLine("Press ENTER to exit...")
            Console.ReadLine()

        End Sub

        Sub Compute()

            Do

                Console.WriteLine(number)

                For i = 0 To number

                    i += 1

                    Console.WriteLine(x)
                    For j = j To i - 1
                        Console.WriteLine(x)
                        Main()
                    Next
                Next


            Loop While (number < 50)

     

        End Sub
    End Module

     

    -----------------------------

    I can't display the triangle partern shown the above. Please show me how to do it in VB 2005.

     

     

    2008年11月1日 11:12

解答

  • Hello Leticiasue,

     

    It looks like an assignment/homework from school to me... but anyway, just want to stimulate your interest in further study in VB.NET, please find the follows code for your refer:

     

    Code Snippet

    Module Module1
        Sub Main()
            Dim number As Integer
            Console.WriteLine("Please input a number:")
            Try
                number = Integer.Parse(Console.ReadLine())
                If number > 0 Then
                    Compute(number, number)
                End If
            Catch ex As Exception
                Console.WriteLine("Invalid input!")
            Finally
                Console.WriteLine("Please ENTER to exit...")
                Console.ReadLine()
            End Try
        End Sub

        Sub Compute(ByVal totalOutput As Integer, ByVal number As Integer)
            For i As Integer = 1 To totalOutput
                If totalOutput - i >= number Then
                    Console.Write(" ")
                Else
                    Console.Write("*")
                End If
            Next
            Console.WriteLine()

            If number > 0 Then
                Compute(totalOutput, number - 1)
            End If
        End Sub
    End Module

     


     

    You may also have the same output in many other ways.  In addition, from your code, please note the follows:

    1. Think carefully whether to use global or local variables

    2. What's the use in calling the procedure "Main" in the subroutine "Compute"?

    3. Is it necessary to fix the number of looping steps to 50 in the subroutine "Compute"?  Or is there any other way to minimize the number of looping steps?

    4. Review the initialization of variable "j" in the inner loop in the subroutine "Compute".

     

    2008年11月1日 12:10

所有回覆

  • Hello Leticiasue,

     

    It looks like an assignment/homework from school to me... but anyway, just want to stimulate your interest in further study in VB.NET, please find the follows code for your refer:

     

    Code Snippet

    Module Module1
        Sub Main()
            Dim number As Integer
            Console.WriteLine("Please input a number:")
            Try
                number = Integer.Parse(Console.ReadLine())
                If number > 0 Then
                    Compute(number, number)
                End If
            Catch ex As Exception
                Console.WriteLine("Invalid input!")
            Finally
                Console.WriteLine("Please ENTER to exit...")
                Console.ReadLine()
            End Try
        End Sub

        Sub Compute(ByVal totalOutput As Integer, ByVal number As Integer)
            For i As Integer = 1 To totalOutput
                If totalOutput - i >= number Then
                    Console.Write(" ")
                Else
                    Console.Write("*")
                End If
            Next
            Console.WriteLine()

            If number > 0 Then
                Compute(totalOutput, number - 1)
            End If
        End Sub
    End Module

     


     

    You may also have the same output in many other ways.  In addition, from your code, please note the follows:

    1. Think carefully whether to use global or local variables

    2. What's the use in calling the procedure "Main" in the subroutine "Compute"?

    3. Is it necessary to fix the number of looping steps to 50 in the subroutine "Compute"?  Or is there any other way to minimize the number of looping steps?

    4. Review the initialization of variable "j" in the inner loop in the subroutine "Compute".

     

    2008年11月1日 12:10
  • Instead of using "Try...Catch", what methods i can use in this program????

    2008年11月1日 12:39
  • Try...Catch... is not the main point in this example.  It is used for exception handling only.  In order to meet the requirement, you can use nested-for-loop instead of recursion as shown in my example.

     

    2008年11月1日 12:56
  • If I don't enter the number in the console, how to check it?

     

     

    2008年11月1日 13:24
  • Try

     

    Code Snippet

            Dim input As String
            input = Console.ReadLine()
            Dim number As Integer

            If Integer.TryParse(input, number) Then
                ' utilize the variable "number"
            End If

     

     

    2008年11月1日 13:30