【问题标题】:Building a base for my christmas tree为我的圣诞树建立基地
【发布时间】:2016-10-22 04:53:26
【问题描述】:

我正在尝试用用户输入的三角形顶部和矩形底座构建一棵圣诞树,但我目前在构建底座时遇到了困难。规格如下:三角形以单个*开头,每行增加*的数量,每行总共增加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 将其居中。

【问题讨论】:

  • 可以添加输出吗?
  • 你希望print("")做什么?

标签: java loops


【解决方案1】:

首先,您需要将结果可视化。

例如,n = 8,所以底边应该是 1 + n/5 = 2 高,n / 3 = 2 调整为 3 宽。

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

您的代码存在以下问题:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      你几乎成功了,只是关于你的代码的几个问题:

      • 最后你应该使用
      • 您已经明确了规则,因此您只需将它们写在代码中,就像我在代码中所做的那样尝试使其更清晰,在写出之前找到树桩的高度和宽度
      • 在内部你应该打印出 * 而在外部你只需要打印一个新行

        扫描仪键盘 = 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
            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();
            }
        

      【讨论】:

        猜你喜欢
        • 2021-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-23
        • 2014-01-13
        • 1970-01-01
        相关资源
        最近更新 更多