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

1. Diamond perimeter with loop

Hi. I've beed trying to do this for days but I can't.

I got the program to do the whole diamond but I don't know how to take out all the asterisk. żAny idea?

This is my code:

```public class diamond
{
public static void main(String args[])
{
int i=0,j,k,n=4;
for(k=1;k<=n;k++) {
for(i=0;i<=n-k;i++) {
System.out.print(" ");
}
for(j=1;j<=k;j++) {
System.out.print("*");
}
for(j=1;j<=k-1;j++) {
System.out.print("*");
}
System.out.println("");
}
for(k=1;k<=n;k++) {
for(i=0;i<=k;i++) {
System.out.print(" ");
}
for(i=1;i<=n-k;i++) {
System.out.print("*");
}
for(j=0;j<=n-k-2;j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}```

2. Re: Diamond perimeter with loop

Apart from the first and last line, each line has: N spaces, star, M spaces, star. All you have to do is work out what N and M is for each line.

3. Re: Diamond perimeter with loop

Inside the outer loop, keep a flag, which will keep track if the very first * has been printed, don't let the program draw * again, unless it's the last index of inner loop.
Note: Don't forget to reset the flag.

4. Re: Diamond perimeter with loop

Originally Posted by Mr.777
Inside the outer loop, keep a flag, which will keep track if the very first * has been printed, don't let the program draw * again, unless it's the last index of inner loop.
Note: Don't forget to reset the flag.