Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: printing asterisks shape in java

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default printing asterisks shape in java

    i am supposed to Write a program that prints a shape similar to the following shape. Maximize use of loops and minimize use of print statements.
    .....*
    ....**
    ...***
    ..****
    ...***
    ....**
    .....*
    (without the "...")(i couldn't get it to stay in shape)


    this is what i got so far
    package assignment7;
     
    public class Exercise3 
    {
    	 public static void main (String[] args)
    	 {
    	  for (int count =0; count < 4; count++)
    	  {
    	    for (int j=0; j < count+1; j++)
    	       System.out.print("*");
    	    System.out.println();
    	  }
    	  for (int count =3; count >= 0; count--)
    	  {
    	    for (int j=0; j < count-1; j++)
    	       System.out.print("*");
    	    System.out.println();
    	  }
    	 }
    }

    this compiles to:
    *
    **
    ***
    ****
    **
    *

    how do i center it to create the shape?


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: printing asterisks shape in java

    I think you need to print spaces too. so that your first asterisk would be placed at the center

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: printing asterisks shape in java

    i don't understand what you mean by that. sorry i am kind of new at this.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,306
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: printing asterisks shape in java

    Each line contains a certain number of spaces with a certain number of asterisks, and there's a pattern:

    2 spaces, 1 asterisk
    1 space, 2 asterisks
    0 spaces, 3 asterisks
    etc . . .

    Determine the pattern and use the loops to create the pattern of spaces and asterisks.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    ATB (April 6th, 2014)

Similar Threads

  1. Asterisks
    By whome in forum What's Wrong With My Code?
    Replies: 15
    Last Post: July 18th, 2012, 12:12 PM
  2. New to Java, Shape Won't Show in Console
    By BulgeBracket in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2012, 07:30 PM
  3. Printing a Diamond shape Problem.
    By Sev in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 23rd, 2011, 09:05 AM
  4. Inverted Equilatieral Triangle (Asterisks) - Not Equilateral :-(
    By HapticThreek in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2011, 06:03 PM
  5. Replies: 1
    Last Post: September 28th, 2011, 08:29 AM