【问题标题】:Centering text in console C# [closed]在控制台 C# 中居中文本 [关闭]
【发布时间】:2015-12-07 06:22:13
【问题描述】:

目前我正在使用这种方法在控制台中将文本居中。

string x = "Whatever I want to print goes here";
Console.SetCursorPosition((Console.WindowWidth - InvalidNo.Length) / 2, Console.CursorTop);
Console.WriteLine(x);

但是我现在需要将看起来像这样的东西居中:

players[PlayerNo].Name + " has stolen a piece of cheese from " + players[choice - 1].Name

类似的东西

"Player {0}", playernum + 1

有什么办法可以用我的方法做到这一点,还是我必须使用不同的方法?

提前致谢。

【问题讨论】:

  • 我没有看到那里的方法,只是一个代码 sn-p 甚至无法编译。那么你说的方法从外面看起来怎么样?
  • @Bart:请改进问题以提供尽可能多的信息。如果您是新来的,请先进行实地考察; stackoverflow.com/tour

标签: c# console centering


【解决方案1】:

你可以用你的方法做到这一点,你只需要先创建字符串,这样你就知道它有多长:

string x = players[PlayerNo].Name + " has stolen a piece of cheese from " + players[choice - 1].Name;

然后你就可以使用剩下的代码了。

对于第二个示例,它使用了采用格式字符串的WriteLine 方法的重载,因此您将使用String.Format 方法来创建字符串:

string x = String.Format("Player {0}", playernum + 1);

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多