I am tasked to make a fence drawing program. So far I can have the user input values to set height and width and using nested for loops I can get it to output the height and width as a "#" symbol. The problem is I need it to have a border of # with | in the middle.
Currently a 4 height 4 width fence looks like this
####
####
####
####
I need it to look like this
####
#||#
#||#
####
public void draw() {
for (i = 1; i < height-1; i++) {
System.out.print("#");
for (j = 1; j < width-1; j++) {
System.out.print("|");
}
System.out.println("#");
}
fenceCount++;
}
When is it ok to use a break. I have a loop asking if the user would like to build another fence type y or n. At the end for the terminating user input "n" does anyone know if there is a better way to do this than a break; ? and is a single break "ok" here? I have heard breaks are sloppy in loops. Also I always feel like I have to reverse my thinking on while loops.
Change draw()
to:
public void draw() {
String border = String.format("%0" + width +"d", 0).replace("0", "#");
System.out.println(border); // top-border
for (int i = 1; i < height-1; i++) {
System.out.print("#");
for (int j = 1; j < width-1; j++) {
System.out.print("|");
}
System.out.println("#");
}
System.out.println(border); // bottom-border
}