Diamond perimeter with loop
Hi. I've beed trying to do this for days but I can't.
http://img831.imageshack.us/img831/8334/diamondc.png
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:
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");
}
}
}
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.
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.
Re: Diamond perimeter with loop
Quote:
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.
Your refering about IF and else statement right?
Re: Diamond perimeter with loop
yes that is what he is saying :)
Re: Diamond perimeter with loop
Quote:
Originally Posted by
Rage1337
Your refering about IF and else statement right?
Yes, sorry for not making it clear.