简体   繁体   English

使用嵌套的循环绘制圣诞树

[英]Drawing christmas trees using nested for loops

I am trying to draw christmas trees that would look something like this in the console, depending on the height the user enters in. here are the rules. 我试图根据用户输入的高度在控制台中绘制看起来像这样的圣诞树。这是规则。 Top part: The rectangle/triangle has as many rows as the number the user entered. 顶部:矩形/三角形的行数与用户输入的数目相同。 The width of the rectangle/triangle is one less than twice the height of the tree (for example, the tree of height 5 has a width of 9). 矩形/三角形的宽度小于树的高度的两倍(例如,高度为5的树的宽度为9)。 In the case of triangle, the base of the top part is right up against the left margin, and each line above it is indented one space further, and is two characters shorter. 在三角形的情况下,顶部的底部正对着左边缘,并且其上方的每一行都缩进了一个空格,并且短了两个字符。 The result is an isosceles triangle that looks a bit like the top of a spruce or fir tree. 结果是一个等腰三角形,看起来有点像云杉或枞树的顶部。 Bottom part: The rectangle below is centred under the rectangle (for Flat tree) or triangle (for Xmas tree). 底部:下面的矩形位于矩形(对于Flat树)或三角形(对于Xmas树)下方居中。 Its height is one more than one fifth of the height of the top part. 它的高度是顶部高度的五分之一。 For example, the rectangle of the tree above has two rows, since 9 ÷ 5 + 1 is 2. The width of the rectangle is one third of the width of the tree -- but add one if that width comes out even. 例如,上面的树的矩形有两行,因为9÷5 +1是2。矩形的宽度是树的宽度的三分之一-如果宽度均匀,则加一。 For example, the triangle of height 5 has a width of 9, so the width of the rectangle is 3 (that's 9 ÷ 3). 例如,高度为5的三角形的宽度为9,因此矩形的宽度为3(即9÷3)。 The tree of height 4, however, has a base of width 7. Its rectangle would be 3 wide (ie, 7 ÷ 3 is 2, which is even, so change it to 3). 但是,高度为4的树的宽度为7。其矩形将为3宽(即7÷3为2,是偶数,因此将其更改为3)。

How tall should the top of the tree be? 7
Flat Tree:
*************
*************
*************
*************
*************
*************
*************
    *****
    *****
Xmas Tree:
      *
     ***
    *****
   *******
  *********
 ***********
*************
    *****
    *****

At the moment this is my code, TreeStructures.java 目前,这是我的代码TreeStructures.java

import java.util.Scanner;

public class TreeStructures {

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int height;
    System.out.print("How tall should the top of the tree be? ");
    height = scnr.nextInt();
    int topWidth = (height * 2) - 1;
    int bottomWidth = topWidth/3;
    System.out.println();
    if (height >= 5 && height <= 20) {
        if(bottomWidth % 2 == 0) {
            bottomWidth = (topWidth/3) + 1;
        }
        // FLAT TREE -----------------------------------------
        System.out.println("Flat tree:");
        // first for loop to print number of rows
        for (int i = 1; i <= height; i++) {
            // second for loop to print stars to create rectangle
            for (int stars = 1; stars <= topWidth; stars++) {
                System.out.print("*");
            }
            // println to print rows in.
            System.out.println();
        }
        // first for loop to print out rows for the bottom part of tree
        for (int i = 0; i <= (height / 5) + 1; i++) {
            // for loop to print the bottom part of the tree
            for (int j = 0; j <= bottomWidth - 1; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= bottomWidth - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // XMAS TREE --------------------------------------------
        System.out.println("Xmas tree:");
        // NESTED LOOPS
        // first for loop to print amount of rows
        for (int i = 0; i < height; i++) {
            // second for loop for print out spaces to match the tree level
            for (int j = 1; j < height - i; j++) {
                System.out.print(" ");
            }
            // third for loop to print out stars
            for (int k = 0; k < (2 * i + 1); k++) {
                System.out.print("*");
            }
            System.out.println();
        }
        // first for loop to determine amount of rows for bottom
        for (int i = 0; i <= (height / 5); i++) {
            // for loop to print the bottom part of the tree
            for (int j = 0; j <= bottomWidth - 1; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= bottomWidth - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    } else {
        System.out.println("Sorry, i can only take heights between 5 and 20"
                + "\nQuitting now...");
    }


}

}

see as of now i have the trees done, and i have the bottom part in there, but the math is off for some reason, i thought it would work but somethings up, second thing, i dont know how to add the spaces for the bottom part to be centered under the top part of the tree. 从现在开始,我已经完成了树木的准备工作,并且底部已经完成,但是由于某种原因,数学运算已关闭,我认为它可以正常工作,但是第二件事,我不知道如何为该空间添加空间底部位于树的顶部下方。

my output right now looks like this. 我的输出现在看起来像这样。

How tall should the top of the tree be? 8
Flat tree:
***************
***************
***************
***************
***************
***************
***************
***************
     *****
     *****
     *****
Xmas tree:
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
     *****
     *****

but when i enter any other number like 6, 5 , 8.. the bottom isnt centered 但是当我输入其他任何数字(例如6、5、8)时,底部未居中

As you have 1/3 for the stem the spaces on both sides are also 1/3 of the whole width. 当您的茎有1/3时,两侧的空间也是整个宽度的1/3。 The general formula would be (width-stemWidth)/2 which results in 1/3 width in this case. 通用公式为(width-stemWidth)/ 2,在这种情况下,其宽度为1/3。

I removed the modula and replaced it by "(height -1) / 5 +1" - it rounds up to next bigger int. 我删除了模数,并将其替换为“(height -1)/ 5 +1”-将其舍入到下一个更大的int。 So if height is 1,2,3,4 or 5 it gives 1(that is including 0: 2 rows), and if height is in 6,7,8,9 or 10 it gives 2(that is including 0: 3 rows) and so on. 因此,如果height为1,2,3,4或5,则得到1(包括0:2行),如果height为6,7,8,9或10,则得到2(包括0:3)。行)等等。 I am not sure but thought thats what you wanted to achieve with the modula. 我不确定,但是这就是您想通过模数实现的目标。

import java.util.Scanner;

public class TreeStructures {

    static Scanner scnr = new Scanner(System.in);
    static int height;

    public static void main(String[] args) {

        System.out.print("How tall should the top of the tree be? ");
        height = scnr.nextInt();
        System.out.println();
        if (height >= 5 && height <= 20) {
            System.out.println("Flat tree:");
            flatTree();
            System.out.println("Xmas tree:");
            xmasTree();
        } else {
            System.out.println("That's not a valid size. I can only do trees from 5 to 20");
            System.out.println("Quitting now.");
        }

    }

    public static void flatTree() {
        int width = (height * 2) - 1;
        // first for loop to print number of rows
        for (int i = 1; i <= height; i++) {
            // second for loop to print stars to create rectangle
            for (int stars = 1; stars <= width; stars++) {
                System.out.print("*");
            }
            // println to print rows in.
            System.out.println();
        }
        //first for loop to print out rows for the bottom part of tree
        for (int i = 0; i <= height / 5; i++) {
            if (height % 2 == 0) {
                for (int j = 0; j <= ((width) / 3) + 1; j++) {
                    System.out.print("*");

                }
            } else {

                //second for loop to print out width for the bottom part of the tree
                for (int j = 0; j <= (width) / 3; j++) {
                    System.out.print("*");

                }
            }
            System.out.println();
        }

    }

    public static void xmasTree() {
        int width = height * 2 - 1;
        // NESTED LOOPS
        // first for loop to print amount of rows
        for (int i = 0; i < height; i++) {
            // second for loop for print out spaces to match the tree level
            for (int j = 0; j < height - i; j++) {
                System.out.print(" ");
            }
            // third for loop to print out stars
            for (int k = 0; k < (2 * i + 1); k++) {
                System.out.print("*");
            }
            System.out.println();
        }
        // first for loop to determine amount of rows for bottom
        for (int i = 0; i <= (height-1) / 5 +1 ; i++) {
                // for loop to print the bottom part of the tree
                for (int j = 0; j <= width/3; j++) {
                    System.out.print(" ");
                }
                for (int j = 0; j <= (width) / 3; j++) {
                    System.out.print("*");
                }
                System.out.println();
        }

    }

}

output: 输出:

How tall should the top of the tree be? 10

Flat tree:
*******************
*******************
*******************
*******************
*******************
*******************
*******************
*******************
*******************
*******************
********
********
********
Xmas tree:
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
       *******
       *******
       *******

If you want it centered nicely in any case, you need to cheat a little and vary the stem width. 如果您希望它在任何情况下都能很好地居中,则需要稍微作弊并改变茎的宽度。

To do so: We know that the tree should be like: 这样做:我们知道树应该像这样:

witdh = gap * stemwidth + gap witdh =间隙*茎宽+间隙

From that we can easily deduct, the gap is 由此我们可以轻松推断出差距是

gap = ( with-stemwidth)/2 间隙=(with-stemwidth)/ 2

Now inserting that again the width is: 现在再次插入宽度为:

width = ( with-stemwidth)/2 + stemwidth + ( with-stemwidth)/2 宽度=(with-stemwidth)/ 2 +茎宽+(with -stemwidth)/ 2

From that we can deduct: 从中我们可以得出:

stemwidth = witdh - 2*(( with-stemwidth)/2). stemwidth = witdh-2 *((with -stemwidth)/ 2)。

Well one could evaluate the right and show that this equation is correct. 好吧,人们可以评估该权利,并证明这个方程是正确的。

In discrete mathematics we need to take raounding and remainders into account. 在离散数学中,我们需要考虑余数和余数。

When calculating the gap a rounding happens in the division and leaves some remainder which is lost. 在计算间隙时,除法中会进行四舍五入,并留下了一些剩余部分,这些部分会丢失。

So with the formula above we calculate a new stemwidth that in discrete adds that lost remainder to the stemwidth again. 因此,使用上面的公式,我们计算出一个新的茎宽,该茎宽离散地增加,再次使茎宽失去了余数。 Making it a little bigger - but centered. 使它更大一点-但要居中。

public static void xmasTree() {
    int width = height * 2 - 1;
    int stem = width - 2*width/3; 
    // NESTED LOOPS
    // first for loop to print amount of rows
    for (int i = 0; i < height; i++) {
        // second for loop for print out spaces to match the tree level
        for (int j = 0; j < height - i; j++) {
            System.out.print(" ");
        }
        // third for loop to print out stars
        for (int k = 0; k < (2 * i + 1); k++) {
            System.out.print("*");
        }
        System.out.println();
    }
    // first for loop to determine amount of rows for bottom
    for (int i = 0; i <= (height - 1) / 5 + 1; i++) {
        // for loop to print the bottom part of the tree
        for (int j = 0; j <= width / 3; j++) {
            System.out.print(" ");
        }
        //here we put the formula to use, instead of using width/3, the equivalent is used, that takes rounding into account.
        for (int j = 0; j < width - 2*(width/3); j++) {
            System.out.print("*");
        }
        System.out.println();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM