Draw a square on the screen:
Input:
Enter the square side length N (3 < = N < = 20) and a certain character C that makes up the square side in one line, with a space between them.
Output:
Outputs the square drawn by the given character C. But notice that the row spacing is larger than the column spacing, so to make the result look more like a square, the number of rows we output is actually 50% of the number of columns (rounded).
import java.util.Scanner; /** * Created with InteIIiJ IDEA. * Description: * User: * Date:2019-07-15 * Time:11:25 */ public class TestDemo1 { public static void function(int a, String c) { int tmp = a / 2;//The number of rows is actually 50% of the number of columns //Rounding if (a % 2 != 0) { tmp = a / 2 + 1; } //When the input is 4, only two lines are printed, each line is a character c if(tmp == 2){ for (int i = 0; i < a; i++) { System.out.print(c); } System.out.println(); for (int i = 0; i < a; i++) { System.out.print(c); } }else{ //First line print a whole line for (int i = 0; i < a; i++) { System.out.print(c); } System.out.println(); //In the middle, the beginning and the end of each line are the characters c, and in the middle are the spaces for (int i = 0; i < tmp-2; i++) { System.out.print(c); for (int j = 0; j < a-2; j++) { System.out.print(" "); } System.out.print(c); System.out.println(); } //Print a whole line on the last line for (int i = 0; i < a; i++) { System.out.print(c); } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); String c = scanner.next(); function(a, c); } }