C# 将一对字符(*)放置在中间而不是左侧

C# 将一对字符(*)放置在中间而不是左侧,c#,for-loop,char,C#,For Loop,Char,因此,我正在努力使用控制台应用程序。它要求字符线形成树状图案,而不是右翼三角形。像这样(3): 到目前为止,我有: int rows = int.Parse(Console.ReadLine()); for(int i = 1; i <= rows; i++) { for(int x = 1; x <= i; x++) { Console.Write("*"); }

因此,我正在努力使用控制台应用程序。它要求字符线形成树状图案,而不是右翼三角形。像这样(3):

到目前为止,我有:

int rows = int.Parse(Console.ReadLine());

for(int i = 1; i <= rows; i++)
 {
            for(int x = 1; x <= i; x++)
            {
                Console.Write("*");
            }
            Console.WriteLine("\n");
 }
int rows=int.Parse(Console.ReadLine());

对于(int i=1;i我想您正在寻找
PadLeft
函数。它在字符串的左侧添加空格,这样您就可以正确定位它。此外,您需要将行数乘以2,并将步长增加1。您将得到以下代码:

int rows = int.Parse(Console.ReadLine()) * 2;

for (int i = 1; i <= rows; i += 2) {
  Console.Write( "".PadLeft( (rows - i) / 2) );
  for(int x = 1; x <= i; x++) {
    Console.Write("*");
  }
  Console.WriteLine();
}
…并将其更改为:

Console.WriteLine();
(这将删除不必要的换行符,
WriteLine
已打印换行符)

Console.WriteLine("\n");
Console.WriteLine();