131k views
23 votes
Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:

XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
INPUT and PROMPTS. The program prompts for an integer as follows: "Enter an integer in the range of 1-15: ".

OUTPUT. The output should be a square of X characters as described above.

CLASS NAMES. Your program class should be called SquareDisplay

User Hudi
by
5.0k points

1 Answer

8 votes

Answer:

import java.util.Scanner;

class SquareDisplay {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Enter an integer in the range of 1-15: ");

int num = scan.nextInt();

if ((num > 0) && (num <= 15)) {

String s = "X".repeat(num) + "\\";

System.out.print(s.repeat(num));

} else {

// your error handling

}

scan.close();

}

}

Step-by-step explanation:

User Jafar Isbarov
by
5.6k points