答案:主体代码中所涉及到的函数
/// <summary>
/// 打印有规则的图形
/// </summary>
/// <param name="begin"></param>
private static void Print(int begin, int end, out int maxlength)
{
maxlength = end * 2 - 1 + (end - 1) * 2 - 1;
int temp = end % 2 == 0 ? 1 : 2;
for (int i = begin; i <= end; i++)
{
int count = i * 2 - 1;
for (int j = 0; j < maxlength / 2 - i; j++)
{
Console.Write(" ");
}
for (int k = 0; k < count; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
static void SetColor(ConsoleColor c)
{
Console.ForegroundColor = c;
}
控制台主体代码。
static void Print()
{
//设置maxLength可以画出不同大小的树
int maxLength = 4;
//打印蓝色
SetColor(ConsoleColor.Blue);
int max = 0;
Print(1, maxLength, out max);
SetColor(ConsoleColor.Red);
Print(3, maxLength, out max);
SetColor(ConsoleColor.DarkRed);
for (int i = 0; i < max; i++)
{
Console.Write("*");
}
Console.WriteLine();
SetColor(ConsoleColor.Green);
for (int j = 0; j < max / 2 - 1; j++)
{
Console.Write(" ");
}
Console.Write("**");
Console.WriteLine();
SetColor(ConsoleColor.White);
for (int j = 0; j < max / 2 - 1; j++)
{
Console.Write(" ");
}
Console.Write("**");
}
OK搞定。
附图
上一个:C#修改登录密码的代码
下一个:JAVA与C#有什么区别