House Drawing Using java (console)

I need to create a program that asks the user to enter the height and length (both are even numbers), and then the program will draw the house. The roof of the house (width / 2) the number of rows. An example program should look like this:

Enter height and width of the house you want me to draw (must be even numbers): 10 10
....**
.../..\
../....\
./......\
/........\
----------
|........|
|........|
|........|
|........|
|........|
|........|
|........|
|........|
|........|
|........|
----------

but this is what I keep getting only for the roof, using 10 and 10 for width and height (I haven't started the body yet):

height: 10
width: 10

....**
.../..\
.../..\
.../..\
.../..\
----------

Does anyone know how I can place the right amount of space so that it looks like a pattern? my code is:

 import java.util.Scanner;

public class QuestionCode {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int height = 0, width = 0;

        String space = ".", left = "/", right = "\\";

        System.out.print("height: ");
        height = keyboard.nextInt();
        System.out.print("width: ");
        width = keyboard.nextInt();


        System.out.println();

        int outerSpace = ((width/2)-1);

        int halfWidth = ((width/2)-2);

        while (outerSpace > 0) {
            System.out.print(space);
            --outerSpace;
        }

        System.out.println("**");

        while (halfWidth >= 0) {

            outerSpace = ((width/2)-2);

            while (outerSpace > 0) {
                System.out.print(space);
                --outerSpace;
            }

            System.out.print(left);
            int innerSpace = 2;

            while (innerSpace < (width-2)) {
                System.out.print(space);
                innerSpace *= 2;
            }

            System.out.println(right);
            halfWidth--;
        }

        while (width > 0) {
            System.out.print("-");
            --width;
        }
    }
}
+4
source share
1 answer

Here you will find a suggestion for this using loops for.

, , .

, , width height, :

heightRoof: , .

middle: , .

, - , , , - .

        int width = 10;
        int height = 10;

        int middle = width % 2 == 0 ? (width / 2) - 1 : width / 2;
        int heightRoof = middle+1;

        for (int i = 0 ; i < heightRoof ; i++){
            for (int j = middle - i - 1 ; j >= 0 ; j--){
                System.out.print(".");
            }
            if (i == 0) {
                System.out.print("**");
                System.out.println();
                continue;
            } else {
                System.out.print("/");
                for (int k = 0 ; k < 2*i ; k++){
                    System.out.print(".");
                }
            }
            System.out.print("\\");
            System.out.println();
        }

        for (int i = 0 ; i < width ; i++){
            System.out.print("-");
        }
+2

Source: https://habr.com/ru/post/1610126/


All Articles