C# 打印星号菱形
代码如下
Console.Write("请输入要打印的行数(奇数):");
int n = int.Parse(Console.ReadLine()) / 2 + 1; //这样就算出上部分的总行数了
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int i = 1; i < n; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= (2 * n - 1) - 2 * i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
int n = int.Parse(Console.ReadLine()) / 2 + 1; //这样就算出上部分的总行数了
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int i = 1; i < n; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= (2 * n - 1) - 2 * i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
效果如下