Hollow square Learn programming Java

Lesson:

Flow Control


Exercise:

Hollow square


Objetive:

Write a java program that asks for a symbol and a width, and displays a hollow square of that width using that symbol for the outer border, as shown in this example:

Enter a symbol: 4
Enter the desired width: 3

444
4 4
444


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int n;
		int i, j;

		System.out.print("Enter number for the border of the square: ");
		n = Integer.parseInt(new Scanner(System.in).nextLine());

		System.out.print("Enter total: ");
		width = Integer.parseInt(new Scanner(System.in).nextLine());

		for (i = 0; i; i++)
		{
			System.out.print(n);
		}

		System.out.println();

		for (i = 0; i - 2; i++)
		{
			System.out.print(n);

			for (j = 0; j - 2; j++)
			{
				System.out.print(" ");
			}

			System.out.println(n);
		}

		for (i = 0; i; i++)
		{
			System.out.print(n);
		}

		System.out.println();
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.