Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By wsaryada

Thread: How to produce a large N that is # characters tall

  1. #1
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default How to produce a large N that is # characters tall

    Hello. I need to learn how to write a program that asks the user for a value of size until a positive integer is entered. After the positive integer is entered, the program produces a large N that is size characters tall.

    All I need to know is how to get the output in that format. I think I use a for loop, but I'm not sure what to use to make the characters align in the shape of a N. I would appreciate any help on this. Thanks!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: How to produce a large N that is # characters tall

    You can use the java.util.Scanner object to get user input. Use the while loop to repeat until the user enter a valid number.

    To draw the N letter you can utilize a matrix / two dimension array to store the letter structure. Then you need to define how to draw the vertical part of the N letter and the diagonal part of the N letter. Pencil and paper drawing can help you to find the logic.

  3. #3
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: How to produce a large N that is # characters tall

    Quote Originally Posted by wsaryada View Post
    To draw the N letter you can utilize a matrix / two dimension array to store the letter structure.
    The problem is from a Practice exam, and it says I just need to write the main method. It says assume the scanner class has been imported, but it doesn't say anything about any other class or Java util. This leads me to believe that it can be done without any other imported Java util besides Scanner.

    Quote Originally Posted by wsaryada View Post
    Then you need to define how to draw the vertical part of the N letter and the diagonal part of the N letter. Pencil and paper drawing can help you to find the logic.
    That's what I need to learn how to do. I don't know how to make the letter N.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: How to produce a large N that is # characters tall

    Yes, you just need to use the Scanner class which is in the java.util package.

    This is the letter N representation in an array. In this case the size / tall is six # letters.
    Java Code:
      012345
      ------
    0|#    #
    1|##   #
    2|# #  #
    3|#  # #
    4|#   ##
    5|#    #
    Now, you need to define the way to draw it for the size defined by user input.

  5. #5
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: How to produce a large N that is # characters tall

    Quote Originally Posted by wsaryada View Post
    Now, you need to define the way to draw it for the size defined by user input.
    Again, that's the only part I do not know how to do. How do I define the way to draw it for the size defined by user input? That's what I'm asking you, I already know the other stuff.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: How to produce a large N that is # characters tall

    Well, basically I tell you what you need to do to solve it. The user input will define the size of the two dimensional array. On the first and the last column of the array fill it with the '#' symbol. And the calculate the row and column for the diagonal part of the N letter and fill it with the '#' symbol.

    When the loop to fill up the array above is done. You just need to loop it and print the value of the array at the row, column position then you'll have the N letter printed out.

    Now, it is your turn to implement the Java code for that, and let see what I can do to help you.

  7. #7
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: How to produce a large N that is # characters tall

    Okay I see what you're saying. I am going to read the text book on how to print arrays in a structured form, and then I will write the code for the program. Thank you for your help :-)

  8. #8
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: How to produce a large N that is # characters tall

    Here is what I got from my instructor:

    Java Code:
    public static void main(String[] args)
    {
        Scanner kbd = new Scanner(System.in);
        int size = -1;
        int row = -1, col = -1;
        char outputChar = '*';
        char space = ' ';
    
        while (size < 1)
        {
            System.out.println("Enter a positive integer for size: ");
            size = kbd.nextInt();
        }
    
        for (row = 1; row <= size; row++)
        {
            for (col = 1; col <= size; col++)
            {
                if (col == 1 || col == size || col == row)
                    System.out.print(outputChar);
                else
                    System.out.print(space);
            }
            System.out.println();
        }
    }

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: How to produce a large N that is # characters tall

    Yes, that's work. It draws a letter N for you. In this case you just print out the '*' and the empty spaces, without using an array as a temporary storage.
    eviljeff likes this.

Similar Threads

  1. Replies: 10
    Last Post: 07-26-2011, 04:47 PM
  2. Replies: 1
    Last Post: 06-24-2011, 06:36 AM
  3. Using Multiple Panels to Produce an Output
    By GavinCash in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2010, 11:56 AM
  4. Use unicode in iReport to produce reports
    By nasir_7b in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 11:53 AM
  5. text Strings to produce letters
    By dc2acgsr99 in forum New To Java
    Replies: 7
    Last Post: 01-29-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •