Results 1 to 7 of 7
Thread: printing asterisks shape in java
- 03-17-2014, 02:48 AM #1
Member
- Join Date
- Mar 2014
- Posts
- 6
- Rep Power
- 0
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(); } } }
*
**
***
****
**
*
how do i center it to create the shape?Last edited by ATB; 03-17-2014 at 04:14 AM.
- 03-17-2014, 02:52 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: printing asterisks shape in java
Prefix each line with the appropriate number of spaces.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-17-2014, 03:07 AM #3
Member
- Join Date
- Mar 2014
- Posts
- 6
- Rep Power
- 0
Re: printing asterisks shape in java
what does that mean?
- 03-17-2014, 03:11 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: printing asterisks shape in java
Use spaces at the beginning of each line to properly center the asterisks.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-17-2014, 03:30 AM #5
Member
- Join Date
- Mar 2014
- Posts
- 6
- Rep Power
- 0
Re: printing asterisks shape in java
how does that work in the code?
- 03-17-2014, 03:39 AM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: printing asterisks shape in java
Try these out to see what I mean.
Java Code:System.out.println("*"); System.out.println(" *");
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-17-2014, 10:22 AM #7
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
-
Post How to Print a square with asterisks in java with while loop only ?
By amratif in forum New To JavaReplies: 1Last Post: 11-30-2013, 06:15 AM -
Moving Shapes Program. Cannot remove first shape when new shape is clicked.
By VettesRus in forum New To JavaReplies: 3Last Post: 09-16-2013, 09:51 PM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 01:48 PM -
Rotated Shape Object Line weight is not retaining properly in printing
By dorairaj in forum AWT / SwingReplies: 7Last Post: 10-06-2009, 06:58 AM -
printing an "E" out of asterisks via strings
By hokieman07 in forum New To JavaReplies: 1Last Post: 04-08-2008, 06:45 AM
Bookmarks