0

So, I've got an assignment that's asking me to print some shapes (Trees) according to the user input. I got all the shapes correctly but there's a second shape that's supposed to print as well, a bottom square (base of the tree) and that second square is supposed to be centered with the top square (The actual tree).

Here's the code so far:

public class TreeStructures {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        int height = 0;
        int weidth = 0;
        int weidth2 = 0;

        //Introduction
        System.out.println("Tree Structures");
        System.out.println("---------------\n");
        System.out.println("This program prints two tree like structures! ");
        System.out.println("A \"Flat\" tree and a \"Christmas\" tree.");
        System.out.println("You choose how big the trees will be.\n");
        System.out.println("By Daniel Sousa (A00433806)\n");

        //User input
        System.out.print("How tall should the top of the tree be? ");
        height = scnr.nextInt();
        System.out.println();

        //Input check
        if (height > 4 && height < 21) {            
            System.out.println("Flat Tree:");

            //Top Square
            for (int i = 1; i <= height; i++) {
                System.out.println(" ");
                for (int j = 0; j <= (height*2) - 2; j++) {
                    System.out.print("*");
                    weidth = j;
                }
            }
            //Bottom Square
            for (int i = 1; i <= (height/5) + 1; i++) {
                System.out.println(" ");
                for (int k = 1; k <= (weidth - weidth2)/2; k++) {
                System.out.print(" ");
                }
                for (int j = 0; j <= weidth/3; j++) {
                    System.out.print("<");
                    weidth2 = j;
                }

            }
        }

        //Invalid Input
        else {
            System.out.println("That's not a valid size.  "
                    + "I can only do trees from 5 to 20.\n");
            System.out.println("Quitting now.\n");
            System.out.print("Press enter...");
            scnr.nextLine();
            scnr.nextLine();
        }
    }
}
\\\

This is the specific code block that is supposed to create and center the second rectangle:

for (int i = 1; i <= (height/5) + 1; i++) {
            System.out.println(" ");
            for (int k = 1; k <= (weidth - weidth2)/2; k++) {
            System.out.print(" ");
            }
            for (int j = 0; j <= weidth/3; j++) {
                System.out.print("<");
                weidth2 = j;
            }

        }

As output, it creates the second rectangle as expected and even indents properly as well, but the first line of the indentation is generating way too many spaces for a reason that I cannot explain.

Output example (replace the "X" by blank spaces):

\\\
Tree Structures
---------------

This program prints two tree like structures! 
A "Flat" tree and a "Christmas" tree.
You choose how big the trees will be.

By Daniel Sousa (A00433806)

How tall should the top of the tree be? 19

Flat Tree:

************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
XXXXXXXXXXXXXXXXXX<<<<<<<<<<<<< //!-!-!-! <-problematic line
XXXXXXXXXXXX<<<<<<<<<<<<< 
XXXXXXXXXXXX<<<<<<<<<<<<< 
XXXXXXXXXXXX<<<<<<<<<<<<

My question is: How can I make the first line of the second rectangle indent("X") as much as the others. Or even better, how can I make that second rectangle align with the middle of the first rectangle?

Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
s0usa
  • 1
  • 1

2 Answers2

0

You seem to overcomplicate it with the for loops, wouldn't it be easier to use 2 loops (1 for each axis you move on) and just decide with simple if wheter you should print something or not? Like this:

for (int i = 1; i <= (height/5) + 1; i++) {
    System.out.println(" ");
    for (int k = 1; k <= 2*weidth/3; k++) {
        if (k < weidth/3){
            System.out.print(" ");
        } else {
            System.out.print("<");
        }
    }
}
Worthless
  • 531
  • 3
  • 7
0

The problem is on your first iteration weidth2 = 0.

for (int k = 1; k <= (weidth - weidth2)/2; k++) {
    System.out.print(" ");
}

for (int j = 0; j <= weidth/3; j++) {
    System.out.print("<");
    weidth2 = j; //happens after first printing of spaces
}

So what you need to do is set width2 first. (I changed weidth2 to width2)

This:

//Bottom Square
for (int i = 1; i <= (height/5) + 1; i++) {
    System.out.println(" ");
    width2 = width/3;

    for (int k = 1; k <= (width - width2)/2; k++)
        System.out.print(" ");

    for (int j = 0; j <= width/3; j++)
        System.out.print("<");
}

Produces this:

How tall should the top of the tree be? 10

Flat Tree:

******************* 
******************* 
******************* 
******************* 
******************* 
******************* 
******************* 
******************* 
******************* 
******************* 
      <<<<<<< 
      <<<<<<< 
      <<<<<<<

I'm not sure the requirements of the assignment but this tree is 19 * wide, it seems like this is an off by one error and should be 20 * wide. But this solves your alignment problem.