For my java class one of the exercises is to print out a diamond using nested for loops. In the exercise you need to use the minimum amount of outputs, while using nested for loops. The other stipulation is that each output can only output 1 character such as a single space, a single asterisk, or a single end line statement.
I've finished it but i was wondering if there was an easier way to do it, or if anyone has tips on cleaning up my code. It just seems like ended up writing way more than was needed. Any help and tips are greatly appreciated. :)
Here is what the end result needs to look like:
Here is my code:
public class Diamond
{
public static void main(String args[])
{
int b = 11; // secondary asterisk loop counter
int ac = 2; // asterisk count
int sc = 5; // space count
int elc = 2; // end line count
int slc = 1; // space loop count
int sslc = 1; // secondary space loop count
for(int e = 1; e < elc && elc < 12;e++)
{
if(elc <= 6)
{
for(int a = 1; a < ac; a++)
{
for(;sc <= 5 && sc > 0; sc--)
{
System.out.print(" ");
}
System.out.print("*");
}
ac += 2;
sc = 5 - slc;
slc += 1;
}
else if (elc > 6)
{
ac -= 2;
sc = 1;
for (; b < ac ; b++)
{
for(;sc <= sslc && sc > -2; sc++)
{
System.out.print(" ");
}
System.out.print("*");
}
b = 1;
sslc += 1;
}
if(elc != 6)
{
System.out.println();
}
elc += 1;
}
}
}