Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30575 > unrolled thread

Re: PRINTING DIAMOND SHAPE WITH LOOPS!

Started bymastewalchekole2007@gmail.com
First post2014-12-16 23:45 -0800
Last post2014-12-16 23:45 -0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: PRINTING DIAMOND SHAPE WITH LOOPS! mastewalchekole2007@gmail.com - 2014-12-16 23:45 -0800

#30575 — Re: PRINTING DIAMOND SHAPE WITH LOOPS!

Frommastewalchekole2007@gmail.com
Date2014-12-16 23:45 -0800
SubjectRe: PRINTING DIAMOND SHAPE WITH LOOPS!
Message-ID<bfe1ebd3-fe8f-4835-af37-e33d4edf0eba@googlegroups.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");
    }
  }
}

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web