Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将Console.WriteLine与中心对齐_C# - Fatal编程技术网

C# 如何将Console.WriteLine与中心对齐

C# 如何将Console.WriteLine与中心对齐,c#,C#,我试图对齐文本,所以我有一个金字塔,但我就是找不到解决方案 Console.Write("Bitte geben Sie die Anzahl der Zeilen der Pyramide ein: "); int zeilen = Convert.ToInt32(Console.ReadLine()); string raute = "#"; string stern = "*"; int anzahlzeichen = -1; double hilfsvariable = 1; whil

我试图对齐文本,所以我有一个金字塔,但我就是找不到解决方案

Console.Write("Bitte geben Sie die Anzahl der Zeilen der Pyramide ein: ");
int zeilen = Convert.ToInt32(Console.ReadLine());
string raute = "#";
string stern = "*";
int anzahlzeichen = -1;
double hilfsvariable = 1;

while(zeilen > 0)
{
    anzahlzeichen += 2;
    int x = anzahlzeichen;

    while(x > 0)
    {
        if (hilfsvariable%2 == 0)
        {
            Console.Write("{0}", stern);
            x--;
        }
        else
        {
            Console.Write("{0}", raute);
            x--;
        }
    }

    hilfsvariable += 1;
    Console.WriteLine();
    zeilen--;
}
现在它从左侧开始打印所有内容

有人知道怎么把它对准中心吗


谢谢你

它的中心将是字符串:

string str = "Hello";
Console.SetCursorPosition((Console.WindowWidth - str.Length) / 2, Console.CursorTop);
Console.WriteLine(str);

它将在中心有一根绳子:

string str = "Hello";
Console.SetCursorPosition((Console.WindowWidth - str.Length) / 2, Console.CursorTop);
Console.WriteLine(str);

啊,我记得这些家庭作业问题。。。您可能需要确定在每个级别插入多少空格字符。请,下次,您可以尝试用英语提供代码吗?可能重复的啊我记得这些家庭作业问题。。。您可能需要确定在每个级别插入多少空格字符。请下次尝试提供英文代码?可能的重复