The work I am currently doing requires me to 'draw' a square by printing '*' as the outline and '.' to fill it in. My code seems to work but it still isn't accepting it. I am a beginner, thanks.
class Main {
public static void main(String args[]) {
int square = 5;
int line = 1;
int stars = 1;
int startLine = square / square;
while (line <= startLine) {
while (stars <= square) { // first line prints out dots for the value of square, in this case 5
System.out.print("*");
stars = stars + 1;
}
System.out.println();
line = line + 1; //prints new line and adds 1 to line. Meaning it will move to the next section as it is now greater than startLine
}
stars = 1; //resets stars
while (line <= square - 1) {// line will keep looping until its value is greater than square - 1
while (stars <= startLine) {
System.out.print("*"); // First character to print is *
stars = stars + 1;
}
while (stars <= square - 1) {
System.out.print("."); //prints 3 dots
stars = stars + 1;
}
while (stars <= square) {
System.out.print("*"); // stars is equal to square so 1 star is printed
stars = stars + 1;
}
stars = 1;
System.out.println();
line = line + 1; // adds a value and loops around again
}
stars = 1;
while (line <= square) {
while (stars <= square) {
System.out.print("*");
stars = stars + 1;
}
System.out.println();
line = line + 1;
}
}
}
It seems to make up the square and if I change the value of 'square' the size changes too. I can't see the problem.