I need to write a program to output an ASCII art pattern. The size of the pattern should change dynamically based on class constants.
It should look like this:
Number of boxes: 4
Width of boxes: 6
Height of boxes: 3
+------+------+------+------+
| | | | |
| | | | |
| | | | |
+------+------+------+------+
public class testing {
public static void main(String[] args) {
for (int height = 1; height <= 2; height++) {
System.out.println("");
for (int box = 1; box <= 4; box++) {
System.out.print("+");
// System.out.print("|");
for (int width = 1; width <= 6; width++) {
System.out.print("_");
}
}
}
}
}
You are going to need to check if your loop is on the "edge" of a box and add different characters accordingly.
However, above I added a lot of smaller string to the end of an output. Instead it would make more sense to use
StringBuilder()
. Adding Strings together is pretty inefficient and creates a lot of objects to only be used once and tossed. Instead (usingStringBuilder()
):