Create hollow rectangle java

0

I have a problem with an exercise that asks me to make a hollow rectangle made with asterisks, so that the height and width is entered by keyboard.

The code I have is the following:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Introduce la altura");
        int alt = sc.nextInt();
        System.out.print("Introduce la anchura");
        int anch = sc.nextInt();
        String aux = "*";
        for (int i = 0; i < alt-1; i++) {
            System.out.println(aux);
        }
        for (int j = 0; j < anch; j++) {
            System.out.print(aux + " ");
        }
}

I can print the left side and the base but I do not know how to do the rest

    
asked by user66886 20.11.2017 в 12:27
source

2 answers

0

Maybe it's better to "find out" first than character to write ... If the asterisk (*) or the white () And then at the end of each line loop write a line break.

Something like that ... (You can adapt it as you wish)

import java.util.Scanner;

public class Rectangulo {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Introduce la altura: ");
        int altura = sc.nextInt();
        System.out.print("Introduce la anchura: ");
        int anchura = sc.nextInt();
        String aux = "";
        // Imprimir
        for (int alto = 0; alto < altura; alto++) {
            for (int largo = 0; largo < anchura; largo++) {
                // Primera y Ultima Linea (Todo con *)
                if (alto==0 | alto == altura-1) {
                        aux="*";                    
                } else {
                // Zona media (* o blanco)
                    if (largo == 0 | largo == anchura-1) aux="*";
                    if (largo !=0 & largo != anchura-1) aux=" ";
                }
                // Pintamos caracter que corresponda
                System.out.print(aux);
                // Si es final de linea un salto
                if (largo == anchura-1) System.out.print("\n");
            }
        }
    }
}
    
answered by 20.11.2017 в 14:42
0

The same cat but rolled and more agile for large rectangles:

import java.util.Scanner;

public class RectanguloHueco {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Introduce la altura: ");
        int altura = sc.nextInt();
        System.out.print("Introduce la anchura: ");
        int anchura = sc.nextInt();

        StringBuilder sbInterior = new StringBuilder();
        StringBuilder sbExterior = new StringBuilder();
        for (int i = 0; i < anchura; i++) {
            sbExterior.append("*");
            sbInterior.append((i == 0 || i == anchura - 1) ? "*" : " ");
        }
        String exterior = sbExterior.toString();
        String interior = sbInterior.toString();

        for (int i = 0; i < altura; i++) {
            System.out.println((i == 0 || i == altura - 1) ? exterior : interior);
        }
    }
}
    
answered by 20.11.2017 в 18:11