Results 1 to 7 of 7
  1. #1
    ATB
    ATB is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    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:

    Java Code:
    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?
    Last edited by ATB; 03-17-2014 at 03:14 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: printing asterisks shape in java

    Prefix each line with the appropriate number of spaces.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ATB
    ATB is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: printing asterisks shape in java

    what does that mean?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: printing asterisks shape in java

    Use spaces at the beginning of each line to properly center the asterisks.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    ATB
    ATB is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: printing asterisks shape in java

    how does that work in the code?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: printing asterisks shape in java

    Try these out to see what I mean.

    Java Code:
    System.out.println("*");
    System.out.println("    *");
    Figuring out how to do it properly is the crux of the assignment.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: printing asterisks shape in java

    The original post actually already displays the solution; it puts dots in front of the stars and you can almost literally read that as "you need to put this many spaces in front to center the line".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 11-30-2013, 05:15 AM
  2. Replies: 3
    Last Post: 09-16-2013, 08:51 PM
  3. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  4. Replies: 7
    Last Post: 10-06-2009, 05:58 AM
  5. printing an "E" out of asterisks via strings
    By hokieman07 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 05:45 AM

Tags for this Thread

Posting Permissions

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