Results 1 to 8 of 8
  1. #1
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default [SOLVED] Help Printign ASCII Pattern

    Hi, I'm writing a simple program that will print the following two patterns:

    Java Code:
        ########
        #######
        ######
        #####
        ####
        ###
        ##
        #
    
        ########
         #######
          ######
           #####
            ####
             ###
              ##
               #
    However, I've written the method for the code called printPatterns. However, I'm not sure how to call the method within the main() in order to run the program. Here's the code thus far:

    Java Code:
    import Patterns.*;
    import java.io.*;
    import java.util.Scanner;
    import java.lang.String.*;
    
    public class Patterns {
        
        public void printPatterns()
        {
        
        int row=1, j=1;
        char OUTPUT='#';
    
        System.out.println();
        
        for (row = 1; row <= 8; row++)
        {
            for (j = 1; j <= row - 1; j++)
            {
                System.out.print(" ");
            }
            for (j = 1; j <= 9 - row; j++)
            {
                System.out.print(OUTPUT);
            }
        System.out.println();
        }
        
        System.out.println();
        
        for (row = 1; row <= 8; row++)
        {
            for (j = 1; j <= 9 - row; j++)
            {
                System.out.print(OUTPUT);
            }
        System.out.println();
        }
        
        System.out.println();
        
        }
    }
    
        public static void main (String[] args)
        {
            Patterns printPattern = new Patterns();
            System.out.Patterns();
        }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
        public static void main (String[] args)
        {
            Patterns printPattern = new Patterns();
            printPattern.printPatterns();
        }

  3. #3
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    Haha, wow, I'm an idiot...sometimes the easiest answers evade me. Thanks.

  4. #4
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    Ok I've managed to get all that working but I was wondering if you could be of further help. I need to create several ASCII images, two of which I discussed above. One, however, is really bugging me. I can't seem to get the for loop statements to create it properly. the image appears basically like a 'Z'. Something like this:

    Java Code:
    ########
          #
         #
        #
       #
      #
     #
    ########
    Thanks for any help!!

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here it is in much simpler way.

    Java Code:
            int _row = 8;
            int _col = 8;
    
            for(int i = 0; i < _col;i++){
                System.out.print("#");
                if(i ==_row - 1){
                    for(int k = 2;k<_row;k++){
                    System.out.println();
                        for(int j = 0; j<_row -k;j++){
                        System.out.print(" ");
                        }
                        System.out.print("#");
                    }
                }
    
            }
            System.out.println();        
            for(int i = 0;i<_col;i++){
                System.out.print("#");
            }

  6. #6
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    Ok, thanks, I'll implement the simpler method. However, I'm still wondering if you could assist me with the loop for the 'Z' printout? Thanks.

  7. #7
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    Wow I'm sorry I didn't realize that's what that was for, thanks a lot.

    Also, is there a way to simply manipulate that image to form another, perhaps something that looks like an hour glass like so:

    Java Code:
    # # # # # # # #
     #           #
       #       #
          # #
          # #
       #       #
     #           #
    # # # # # # # #

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I think you can try to do it quit similar way I have done to make 'z' shape. There I used a single space between two characters. Here in you shape there should be two spaces.


Similar Threads

  1. How to obtain ASCII code of a character
    By karma in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 02:57 AM
  2. Getting ASCII codes of character
    By gapper in forum New To Java
    Replies: 1
    Last Post: 02-02-2008, 09:42 AM
  3. Printing ASCII values of characters
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:36 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
  •