【问题标题】:Christmas Tree trunk in c#c#中的圣诞树树干
【发布时间】:2021-06-11 07:13:37
【问题描述】:

我正在尝试在 C# 中绘制一棵圣诞树并设法绘制了实际的树,但我在将树干绘制在树的中间时遇到了问题。问题是树干实际上打印在树末端的开始处,而不仅仅是在中间。

        static void Main(string[] args)
    {
        // Get's current console window size
        int origWidth = Console.WindowWidth;

        int spaces = origWidth/2;
        int widthOfTree = 1;

        Console.WriteLine("Enter the height of the desired razor tree");
        int treeHeightUserInput = 0;
        while (!int.TryParse(Console.ReadLine(), out treeHeightUserInput))
        {
            Console.WriteLine("Enter a valid number!");               
        }

        // draws tree
        for (int i = 0; i < treeHeightUserInput; i++)
        {
            // indentation
            for (int j = 0; j < spaces; j++)
            {
                Console.Write(" ");
            }
            for (int j = 0; j < widthOfTree; j++)
            {
                Console.Write("* ");
            }
            Console.WriteLine();
            widthOfTree++;
            // reduces width of next line
            spaces--;
        }

        // draws trunk
        for (int i = 0; i < treeHeightUserInput / 3; i++)
        {
            for (int j = 0; j < spaces; j++)
            {
                Console.Write(" ");
            }
            for (int j = 0; j < widthOfTree / 3; j++)
            {
                Console.Write("| ");
            }
            Console.WriteLine();
        }
    }

看起来就是这样,我不太确定问题出在哪里,因为我几乎重复使用了用于绘制树的相同代码,但只是将高度和厚度减少了 2/3。有人猜吗?

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    所以,spacesdraws tree 循环中趋向 0,这就是使你的树叶呈三角形的原因,但是当你绘制树干时,你继续使用 spaces,就像你完成绘制叶子,意思是你的树干与叶子的左边缘(三角形的左下角)对齐

    如果你想让你的树干偏离与叶子左下角相同的缩进水平,你必须在到达这个循环之前增加spaces

            // indentation
            for (int j = 0; j < spaces; j++)
            {
                Console.Write(" ");
            }
    

    在运行indentation 循环之前,我将把它作为练习让您计算spaces 的合适值

    【讨论】:

    • 不完全是 0 而是 origWidth/2 - userInput
    • @Orace 请问为什么 userInput 在这里很重要?因为我只是使用 userInput 来声明树的高度。或者你的意思是因为如果树的高度不够高,从技术上讲,树可以在空间达到 0 之前结束?
    • 我调整了真实帖子的措辞;我假设为 0,因为我认为无论高度如何,树的左边缘都触及控制台的左边缘。 Orace 观察到 spaces 以 width/2-userinput 结束,因为 spaces 从 origwidth/2 开始,然后每行树高循环减一
    • @Mike_AZ 因为 userInput 还给出了树的宽度,而树干的水平开始和结束位置取决于树的宽度。
    【解决方案2】:

    spaces 在绘制树的顶部时递减 (spaces--)。所以它的值不符合你的需要,你必须在绘制树干的循环之前重置它或增加它(treeHeightUserInput/3)。

    【讨论】:

      【解决方案3】:

      感谢 Caius Jard,我能够在一定程度上解决这个问题。我怀疑这是一种解决问题的干净方法,但它确实有效。

                  // draws trunk
              int trunk = origWidth / 2;
              for (int i = 0; i < treeHeightUserInput / 3; i++)
              {
                  for (int j = 0; j < trunk - ((widthOfTree + 1) / 3); j++)
                  {
                      Console.Write(" ");
                  }
                  for (int j = 0; j < widthOfTree / 3; j++)
                  {
                      Console.Write("| ");
                  }
                  Console.WriteLine();
              }
      

      【讨论】:

      • 为了可读性,您可以将主干重命名为trunkStart,将其初始化为origWidth/2 - ((widthOfTree + 1) / 3),并像这样编写第一个内部for循环:for (int j = 0; j &lt; trunkStart; j++)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 2014-01-13
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多