我想在控制台窗口上显示一条消息,只要该消息不会超过窗口的默认 0-79 X 宽度。我的代码如下所示:
int xRemaining = 80 - mRobot.CurrentPos.X;
string message = "ID{0:d} Facing {1:s} at ({2:d},{3:d}) home is({4:d},{5:d})";
string formatMessage = string.Format(message,mRobot.ID,mRobot.getDir.ToString()/*...*/;
if(mRobot.CurrentPos.Y < 24)
{
if (xRemaining < formatMessage.Length)
{
Console.SetCursorPosition((mRobot.CurrentPos.X - xRemaining), mRobot.CurrentPos.Y+1);
}
else
{
Console.SetCursorPosition(mRobot.CurrentPos.X, mRobot.CurrentPos.Y + 1);
}
}
else
{
if(xRemaining < formatMessage.Length)
{
Console.SetCursorPosition((mRobot.CurrentPos.X-xRemaining), mRobot.CurrentPos.Y-1);
}
else
{
Console.SetCursorPosition(mRobot.CurrentPos.X, mRobot.CurrentPos.Y-1);
}
}
Console.Write(message,,mRobot.ID, mRobot.getDir.ToString(), mRobot.CurrentPos.X, mRobot.CurrentPos.Y,mRobot.Home.X,mRobot.Home.Y);
编辑:使用 string.Format,但似乎仍然运行到下一行:/