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

时间:2019-02-21 23:20:57

标签: java loops for-loop nested

我正在尝试在控制台中绘制圣诞树,看起来像这样,具体取决于用户输入的高度。这是规则。 顶部:矩形/三角形的行数与用户输入的数目相同。宽度 矩形/三角形的高度小于树的高度的两倍(例如,树的高度 5的宽度为9)。在三角形的情况下,顶部的底部在右上方 边距,其上方的每一行都缩进一个空格,并且短两个字符。的 结果是一个等腰三角形,看起来有点像云杉或枞树的顶部。 底部:下面的矩形居中于矩形(对于“扁平树”)或三角形(对于 圣诞树)。它的高度是顶部高度的五分之一。例如, 上面树的矩形有两行,因为9÷5 +1是2。矩形的宽度是1 树的宽度的三分之一-如果宽度均匀就加一。例如, 高度为5的三角形的宽度为9,因此矩形的宽度为3(即9÷3)。的树 高度4的底宽为7。其矩形的宽度为3(即7÷3为2,即 甚至将其更改为3)。

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

这是我的代码, 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...");
    }


}

}

从现在开始,我已经完成了树的准备工作,并且底部已经完成,但是由于某种原因,数学运算已关闭,我认为它可以正常工作,但是第二件事,我不知道如何添加底部在树的顶部下方居中的空间。

我的输出现在看起来像这样。

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

但是当我输入其他任何数字(例如6、5、8)时,底部将不会居中

1 个答案:

答案 0 :(得分:1)

由于您的茎有1/3,所以两侧的间距也是整个宽度的1/3。通用公式为(width-stemWidth)/ 2,在这种情况下,宽度为1/3。

我删除了模数,并用“((height -1)/ 5 +1)”代替-取整到下一个更大的int。因此,如果height为1,2,3,4或5,则得到1(包括0:2行),如果height为6,7,8,9或10,则得到2(包括0:3)。行)等等。我不确定,但是这就是您想通过模数实现的目标。

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();
        }

    }

}

输出:

How tall should the top of the tree be? 10

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

在任何情况下,如果您希望它居中定位,都需要作弊一点并改变茎的宽度。

要这样做: 我们知道这棵树应该像:

witdh =间隙*茎宽+间隙

我们可以轻松推断出差距是

gap =(with-stemwidth)/ 2

现在再次插入宽度为:

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

由此我们可以得出:

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

一个人可以评估这个权利,并证明这个方程是正确的。

在离散数学中,我们需要考虑余数和余数。

在计算间隙时,除法过程中会进行四舍五入,剩下的部分会丢失。

因此,使用上面的公式,我们计算出一个新的茎宽,该茎宽在离散的情况下又增加了剩余的茎宽。使它更大一点-但要居中。

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();

    }