ASCII art diamond exhibition
To get a combined output of diamonds in one line, you can use three nested for loops and a couple of if else statements.
n=1 n=2 n=3 n=4 n=5 n=6 n=7
+--+ +----+ +------+ +--------+ +----------+ +------------+ +--------------+
|<>| | /\ | | /\ | | /\ | | /\ | | /\ | | /\ |
+--+ |<-->| | /--\ | | /--\ | | /--\ | | /--\ | | /--\ |
| \/ | |<====>| | /====\ | | /====\ | | /====\ | | /====\ |
+----+ | \--/ | |<------>| | /------\ | | /------\ | | /------\ |
| \/ | | \====/ | |<========>| | /========\ | | /========\ |
+------+ | \--/ | | \------/ | |<---------->| | /----------\ |
| \/ | | \====/ | | \========/ | |<============>|
+--------+ | \--/ | | \------/ | | \----------/ |
| \/ | | \====/ | | \========/ |
+----------+ | \--/ | | \------/ |
| \/ | | \====/ |
+------------+ | \--/ |
| \/ |
+--------------+
Try it online!
// maximum diamond size
int max = 7;
// title row
for (int n = 1; n <= max; n++) {
System.out.print("n=" + n);
// padding up to the diamond width
for (int a = 0; a < n * 2 - 1; a++)
System.out.print(" ");
// indent after diamond
System.out.print(" ");
}
System.out.println();
// combined rows of diamonds
for (int row = 1; row <= max * 2 + 1; row++) {
// sizes of diamonds
for (int n = 1; n <= max; n++) {
// current diamond row [-n, n]
int i = row - n - 1;
if (i <= n) // elements of the row [-n-1, n+1]
for (int j = -n - 1; j <= n + 1; j++)
if (j == 0) continue; // skip middle vertical
else if (Math.abs(j) == n + 1) // vertical borders & corners
System.out.print(Math.abs(i) == n ? "+" : "|");
else if (Math.abs(i) == n) // horizontal borders
System.out.print("-");
else if (i == 0 && Math.abs(j) == n) // middle left & right tips
System.out.print(j == -n ? "<" : ">");
else if (Math.abs(i - j) == n) // upper right & lower left edges
System.out.print("\\");
else if (Math.abs(i + j) == n) // upper left & lower right edges
System.out.print("/");
else if (Math.abs(i) + Math.abs(j) < n) // inner rhombus lines
System.out.print((n - i) % 2 != 0 ? "=" : "-");
else // whitespace
System.out.print(" ");
else // empty row, current diamond width + borders
for (int a = 0; a < n * 2 + 2; a++)
System.out.print(" ");
// indent after diamond
System.out.print(" ");
}
System.out.println(); // new line
}
See also: How to output an ASCII star patterns in a table format?