Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30575
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2014-12-16 23:45 -0800 |
| References | <bd7dee67.0408241759.3884e4e9@posting.google.com> |
| Message-ID | <bfe1ebd3-fe8f-4835-af37-e33d4edf0eba@googlegroups.com> (permalink) |
| Subject | Re: PRINTING DIAMOND SHAPE WITH LOOPS! |
| From | mastewalchekole2007@gmail.com |
On Tuesday, August 24, 2004 6:59:36 PM UTC-7, Jay Dean wrote:
> I need simple nested loops (either for, or while) code for a method to
> print a diamond shaped stars for n stars, where n is odd.
> Example. n=9 would produce
>
> *
> ***
> *****
> *******
> *********
> *******
> *****
> ****
> *
> I have STRUGGLED with this for over three days in a row and my head
> aches!
> I tried with nested loops, and conditions but the results were not
> encouraging.
> I am a novice in Java and need help with simple nested loops to
> construct this. Thanks in advance for your help !!
>
> Jay Dean
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread
Re: PRINTING DIAMOND SHAPE WITH LOOPS! mastewalchekole2007@gmail.com - 2014-12-16 23:45 -0800
csiph-web