I have a problem to draw a square ASCII with the size limit in 20 character (size of ‘square’), this is my code, I’ve tested and it works but when I put the number more than 20 it’s display an error, pls help, thanks.
class Main {
public static void printSquare(int size) {
int line = 1;
while (line <= size) { // For each line of square
int width = size; // width of square segment
int i = 1; // display square segment
while (i <= width && size <= 20) {
System.out.print("*");
i = i + 1;
}
System.out.println(); // Newline
line = line + 1;
}
}
}
The problem is that you’ll never print the
*if the size is greater than 20. A better way to restrict it to 20 would be to limit the size before the loop.and then edit
to