4.29 (Square of Asterisks) Write an application that prompts the user to enter the size of the side of a square, then displays a hollow square of that size made of asterisks. Your program should work for squares of all side lengths between 1 and 20.
I keep getting the error: "AsteriskSquare.java:2: error: class Main is public, should be declared in a file named Main.java public class Main ^ 1 error"
import java.util.Scanner;
public class PrintSquare{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int sides = 0;
while(sides == 0 || sides > 20){
System.out.println("Enter a number between 1 and 20: ");
int input = sc.nextInt();
if(input > 0 && input <= 20)
sides = input;
}
for(int i=0; i<sides; i++){
if(i == 0 || i == sides-1){
for(int j=0; j<sides; j++){
System.out.print("*");
}
}else{
System.out.print("*");
for(int j=0; j<sides-2; j++){
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
}
}
Get Answers For Free
Most questions answered within 1 hours.