相关疑难解决方法(0)

在C#控制台应用程序中居中文本仅使用某些输入

我在C#.NET4控制台应用程序中居中文本时遇到问题.

这是我将文本居中的方法:

private static void centerText(String text)
{
    int winWidth = (Console.WindowWidth / 2);
    Console.WriteLine(String.Format("{0,"+winWidth+"}", text));
}
Run Code Online (Sandbox Code Playgroud)

但是,我只是输出正常输出的输出.但是,如果我使用此行:

Console.WriteLine(String.Format("{0,"+winWidth+"}", "text"));
Run Code Online (Sandbox Code Playgroud)

"文本"应该居中.

centerText用这两种方法打电话:

private static void drawStars()
{
    centerText("*********************************************");
}
private static void title(string location)
{
    drawStars();
    centerText("+++ Du er nu her: " + location + "! +++");
    drawStars();
}
Run Code Online (Sandbox Code Playgroud)

c# console-application centering c#-4.0

6
推荐指数
1
解决办法
9054
查看次数

标签 统计

c# ×1

c#-4.0 ×1

centering ×1

console-application ×1