我希望控制台输出基本上具有表单的一般外观.
以下是我希望输出显示的方式:
First Name: //using Console.ReadLine() right here while there is other text below
Last Name:
Badge Number:
Run Code Online (Sandbox Code Playgroud)
然后
First Name: Joe
Last Name: //using Console.ReadLine() right here while there is other text below
Badge Number:
Run Code Online (Sandbox Code Playgroud)
最后
First name: Joe
Last name: Blow
Badge Number: //using Console.ReadLine() right here
Run Code Online (Sandbox Code Playgroud)
你需要这个Console.SetCursorPosition()方法.
Console.WriteLine("First Name: ");
Console.WriteLine("Last Name: ");
Console.WriteLine("Badge Number: ");
Console.SetCursorPosition(12, 0);
string fname = Console.ReadLine();
Console.SetCursorPosition(11, 1);
string lname = Console.ReadLine();
Console.SetCursorPosition(14, 2);
string badge = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
为了好玩,让我们更加可重用:
IList<string> PromptUser(params IEnumerable<string> prompts)
{
var results = new List<string>();
int i = 0; //manual int w/ foreach instead of for(int i;...) allows IEnumerable instead of array
foreach (string prompt in prompts)
{
Console.WriteLine(prompt);
i++;
}
//do this after writing prompts, in case the window scrolled
int y = Console.CursorTop - i;
if (y < 0) throw new Exception("Too many prompts to fit on the screen.");
i = 0;
foreach (string prompt in prompts)
{
Console.SetCursorPosition(prompt.Length + 1, y + i);
results.Add(Console.ReadLine());
i++;
}
return results;
}
Run Code Online (Sandbox Code Playgroud)
然后你会这样称呼它:
var inputs = PromptUser("First Name:", "Last Name:", "Badge Number:");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1305 次 |
| 最近记录: |