Función WriteRectangle Aprende programación con ejercicios Java

Lección:

Funciones


Ejercicio:

Función WriteRectangle 90


Objetivo:

Cree una función WriteRectangle para mostrar un rectángulo (relleno) en la pantalla, con el ancho y el alto indicados como parámetros, utilizando asteriscos. Complete el programa de prueba con una función principal:

WriteRectangle(4,3);

debe mostrarse
****
****
****

Cree también una función WriteHollowRectangle para mostrar sólo el borde del rectángulo:
WriteHollowRectangle(3,4);

debe mostrarse
***
* *
* *
***


Código:

import java.util.*;
public class Main
{
	private static void WriteRectangle(int width, int height)
	{
		for (int i = 0; i <= width; i++)
		{
			for (int j = 0; j <= height; j++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}

	private static void WriteHollowRectangle(int width, int height)
	{

		for (int i = 1; i <= height; i++)
		{
			for (int j = 1; j <= width; j++)
			{
				if ((i == 1) || (i == height))
				{
					System.out.print("*");
				}
				else
				{
					if ((j == 1) || (j == width))
					{
						System.out.print("*");
					}
					else
					{
						System.out.print(" ");
					}
				}
			}
			System.out.println();
		}
	}
	public static void main(String[] args)
	{
		WriteRectangle(4, 3);
		System.out.println();
		WriteHollowRectangle(3, 4);
		new Scanner(System.in).nextLine();
	}
}