Printing Pattern

Joined
Jun 18, 2010
Messages
6
Reaction score
0
I have to use nested loops to print the following pattern

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

The lines go 1, 3, 5, 7, 9, 9, 7, 5 ,3 and 1 long and the user choices which character they would like the pattern to be. I have done the user input but with the following code
Code:
    Scanner scanner = new Scanner(System.in);
    //Asks the user what character they would like to use in the pattrern
    System.out.println("Please type which charector you would like to use?");
    String character = scanner.next();

i have tryed a few different codes to get this to work but i am unable to find any help to get this done on the Internet and textbooks..

Code:
    for (int i=1; i<=9; i++)
    {
        for (int j=1; j<=i; j++)
        {
            System.out.print(character);
        }
        for (int k=9; k<=i; k--)
        {
           System.out.print(character);
        }
    }
        System.out.println();
The code above puts all the characters in one line

Thanks in advance
Tomas
 
Last edited:
Joined
Jun 18, 2010
Messages
6
Reaction score
0
ok i have got it printing a flat tringle now but i need to get it to print a dimond

Code:
  public static void pattern()
  {
    Scanner scanner = new Scanner(System.in);
    //Asks the user what character they would like to use in the pattrern
    System.out.println("Please type which charector you would like to use?");
    String character = scanner.next();

    //Draw the pattern
    for (int i=1; i<=9; i++)
    {
        for (int j=1; j<=i; j++)
        {
            System.out.print(character);
        }
        System.out.println();
    }
        

    //Draw the pattern
    for (int i=9; i>=0; i--)
    {
        for (int j=1; j<=i; j++)
        {
            System.out.print(character);
        }
        System.out.println();
    }//End For statment

   }//Pattern

thats my code...
 
Joined
Jun 18, 2010
Messages
6
Reaction score
0
so i have no got it printing every odd numbered line only now i have to get the spacing. Remember if you can help post!

The code is
Code:
 //Draw the top of the dimond
   
    for (int i=1; i<=9; i=i+2)
    {
         for (int j=1; j<=i; j++)
        {
            System.out.print(character);
        }//End Inner Loop
        System.out.println();
    }//End Outer Loop
        
    //Draw the bottom of the dimond
    for (int i=9; i>=0; i=i-2)
    {
        for (int j=1; j<=i; j++)
        {
            System.out.print(character);
        }//End Inner Loop
        System.out.println();
    }//End Outer loop

I am lost for what to do
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top