简体   繁体   English

c#中的圣诞树树干

[英]Christmas Tree trunk in c#

I'm trying to draw a christmas tree in C# and have managed to draw the actual tree, but im having issues with drawing the trunk in the middle of the tree.我正在尝试在 C# 中绘制一棵圣诞树,并设法绘制了实际的树,但我在树中间绘制树干时遇到了问题。 The issue is that the trunk is actually printed at the start of the end of the tree and not just straight in the middle.问题是树干实际上打印在树末端的开始处,而不是直接打印在中间。

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

That's how it looks, I'm not really sure where the issue is as I'm pretty much reusing the same code that I used to draw the tree but just reduced the height and thickness by 2/3.这就是它的外观,我不确定问题出在哪里,因为我几乎重复使用了我用来绘制树的相同代码,但只是将高度和厚度减少了 2/3。 Does anyone have a guess?有人猜吗? 最终结果树

So, spaces goes towards 0 in the draws tree loop, which is what makes your tree leaves triangular, but then when you draw the trunk you carry on using spaces as whatever it became when you finished drawing the leaves, meaning your trunk is aligned with the left edge of the leaves (the bottom left of the triangle)因此,在draws tree循环中, spaces趋向于 0,这就是使树叶呈三角形的原因,但是当您绘制树干时,您继续使用spaces ,就像您完成绘制树叶时所形成的一样,这意味着您的树干与叶子的左边缘(三角形的左下角)

If you want to offset your trunk away from the same indent level as the bottom left corner of the leaves, you'll have to have to increase spaces before you reach this loop:如果你想将你的树干从与叶子左下角相同的缩进水平偏移,你必须在到达这个循环之前增加spaces

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

I'll leave it as an exercise for you to calc a suitable value for spaces before running the indentation loop在运行indentation循环之前,我将把它作为练习让你计算一个合适的spaces

spaces is decremented when the top part of the tree is drawn ( spaces-- ).当绘制树的顶部时, spaces会减少( spaces-- )。 So it's value doesn't match your needs, you have to reset it or increments it (by treeHeightUserInput/3 ) before the loop that draw the trunk.所以它的值不符合您的需求,您必须在绘制树干的循环之前重置它或增加它(通过treeHeightUserInput/3 )。

Thanks to Caius Jard, I was able to somewhat solve the issue.感谢 Caius Jard,我能够在一定程度上解决这个问题。 I doubt that it's a clean way to solve it, but it works.我怀疑这是一种解决问题的干净方法,但它确实有效。

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

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

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