C# 如何对齐控制台菜单中的所有行?
我试过用c写这种菜单: 我唯一的问题是,我得到了各种各样的值,它们不是像这个菜单中的一样永久不变,所以这是我在实践中得到的打印结果:C# 如何对齐控制台菜单中的所有行?,c#,C#,我试过用c写这种菜单: 我唯一的问题是,我得到了各种各样的值,它们不是像这个菜单中的一样永久不变,所以这是我在实践中得到的打印结果: Put a vehicle in the garage ---------------------------> 1 Display Licenses of cars in garage ----------------------------> 2 Change a vehicle status ---------------------------
Put a vehicle in the garage ---------------------------> 1
Display Licenses of cars in garage ----------------------------> 2
Change a vehicle status ---------------------------> 3
Fill tires to maximum ---------------------------> 4
Refuel a vehicle---------------------------> 5
Recharge a vehicle ----------------------------> 6
Show data of a vehicle ---------------------------> 7
Exit ---------------------------> 0
好吧,我希望所有这些都像我的示例顶部一样对齐。
有人建议我怎么做吗?您需要计算每行要使用的破折号数。试试这样的
using System;
public class Program
{
public static void Main()
{
string[] menuPrompts = {
"Put a vehicle in the garage",
"Display Licenses of cars in garage",
"Change a vehicle status",
"Fill tires to maximum",
"Refuel a vehicle",
"Recharge a vehicle",
"Show data of a vehicle",
"Exit"
};
int maxLength = 56;
for (int i = 0; i < menuPrompts.Length; i++)
{
string prompt = menuPrompts[i];
int n = maxLength - (prompt.Length + 1);
string pad = new String('-', n);
Console.WriteLine("{0} {1}> {2}", prompt, pad, i + 1);
}
}
}
使用系统;
公共课程
{
公共静态void Main()
{
字符串[]menupmpts={
“把车放进车库”,
“在车库展示汽车牌照”,
“更改车辆状态”,
“将轮胎加注到最大值”,
“给车辆加油”,
“给车辆充电”,
“显示车辆数据”,
“退出”
};
int maxLength=56;
for(int i=0;i{2}”,提示符,pad,i+1);
}
}
}
你好,非常感谢。我想问,如果我的数组是用户界面不知道的枚举数组,那么是否有办法获得maxLength,因此我无法从高级的MaxLen中确定
using System;
public class Program
{
public static void Main()
{
string[] menuPrompts = {
"Put a vehicle in the garage",
"Display Licenses of cars in garage",
"Change a vehicle status",
"Fill tires to maximum",
"Refuel a vehicle",
"Recharge a vehicle",
"Show data of a vehicle",
"Exit"
};
int maxLength = 56;
for (int i = 0; i < menuPrompts.Length; i++)
{
string prompt = menuPrompts[i];
int n = maxLength - (prompt.Length + 1);
string pad = new String('-', n);
Console.WriteLine("{0} {1}> {2}", prompt, pad, i + 1);
}
}
}