【发布时间】: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。有人猜吗?
【问题讨论】: