为我的圣诞树建立基地

时间:2016-10-21 16:16:22

标签: java loops

我正在尝试用一个用户输入的三角形顶部和一个矩形底座来构建一棵圣诞树,但是我在构建底座时遇到了困难。规格如下:三角形以单个*开头,每行增加*,每行增加2个,使得最后一行为2(n)-1,其中n为输入的高度。三角形。树下面的矩形(树桩)的高度等于1加(1/5)n,树桩的宽度是1 / 3n但是如果1 / 3n的结果是偶数,则将总数加1树桩宽度。 (即树桩宽度总是奇数。) 这是我到目前为止,树的三角形部分很好,但矩形是一团糟。

public class xmasTree {

    public static final int TREE_MAXIMUM = 20;
    public static final int TREE_MINIMUM = 3;

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        //User introduction 
        System.out.println("This program prints a \"Christmas\" tree. You "
            + "choose how big the tree will be (within reason);

        // prompt for and get user input
        System.out.println("How tall should the top of the tree be?");
        int treeHeight = keyboard.nextInt();
        while (treeHeight < TREE_MINIMUM || TREE_MAXIMUM < treeHeight) {
            System.out.println("That's not a valid size.  I can only do trees "
                + "from 3 to 20.");
            System.out.println("Qutting now.");
            System.exit(0);
        }

        //Print the top triangle section of the christmas tree
        for (int i = 0; i < treeHeight; i++) {
            for (int j = 0; j < treeHeight - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k < (2 * i + 1); k++) {
                System.out.print("*");
            }
            System.out.println();
        }
        //Print the bottom rectangle section of the tree
        for (int c = 0; c <= (1 + (treeHeight / 5)); c++) {
            for (int d = 0; d <= (treeHeight / 3); d++) {
                System.out.print("");
            }
            System.out.printf("*");
        }

    }
}

如果有人可以帮我弄清楚如何获得适合矩形的正确输入形状,我知道一旦正确构建,我可以用简单的printf将其居中。

3 个答案:

答案 0 :(得分:0)

首先,您需要想象您的结果应该是什么。

示例n = 8,因此基数应为1 + n/5 = 2高,n / 3 = 2调整为3宽。

        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
       ***
       ***

您的代码存在以下问题:

  • print("")是一个毫无意义的陈述。它什么都不做。
  • 您不打印任何前导空格以进行缩进。
  • 您不打印*计数等于基本宽度。
  • 你不能打电话给println(),所以基本高度肯定不会起作用。

你让树的部分很好,所以你真的不应该有这个问题。我不会给你答案,但希望这个可视化的例子可以帮助你自己做。

答案 1 :(得分:0)

如果你让你的for循环从0开始,那么就不要使用等号,因为它会比你期望的还要多工作一次

答案 2 :(得分:0)

你几乎成功了,只是关于你的代码的几条评论:

  • 在最后你应该使用&lt;而不是&lt; =就像你在beggining上做的那样
  • 您已经清除了规则,因此您只需要在代码中编写它们,就像我在代码中所做的那样尝试使其更清晰,在写出之前找到树桩的高度和宽度
  • 在内心,你应该打印出*,在外面为你打印一条新线

    扫描仪键盘=新扫描仪(System.in);

        //User introduction 
        System.out.println("This program prints a \"Christmas\" tree. You "
            + "choose how big the tree will be (within reason)");
    
        // prompt for and get user input
        System.out.println("How tall should the top of the tree be?");
        int treeHeight = keyboard.nextInt();
        while (treeHeight < TREE_MINIMUM || TREE_MAXIMUM < treeHeight) {
            System.out.println("That's not a valid size.  I can only do trees "
                + "from 3 to 20.");
            System.out.println("Qutting now.");
            System.exit(0);
        }
    
        //Print the top triangle section of the christmas tree
        for (int i = 0; i < treeHeight; i++) {
            for (int j = 0; j < treeHeight - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k < (2 * i + 1); k++) {
                System.out.print("*");
            }
            System.out.println();
        }
        //Print the bottom rectangle section of the tree
        int stumpHeight = (1 + (treeHeight / 5));
        int stumpWidth;
        if ((treeHeight / 3) % 2 == 0) {
            //it is even add 1
            stumpWidth = (treeHeight / 3) + 1;
        } else {
            //it is odd keep it like that
            stumpWidth = (treeHeight / 3);
        }
        for (int c = 0; c < stumpHeight; c++) {
            //sorry for adding the spaces but I don't like the incomplete answers
            //centering
            for (int d = 0; d < treeHeight - stumpWidth/2; d++) {
                System.out.print(" ");
            }
            //adding stump
            for (int d = 0; d < stumpWidth; d++) {
                System.out.print("*");
            }
            //going to the next line
            System.out.println();
        }