我希望控制台输出基本上具有表单的一般外观。
这是我希望输出的样子的表示:
First Name: //using Console.ReadLine() right here while there is other text below
Last Name:
Badge Number:
然后
First Name: Joe
Last Name: //using Console.ReadLine() right here while there is other text below
Badge Number:
最后
First name: Joe
Last name: Blow
Badge Number: //using Console.ReadLine() right here
最佳答案
您需要 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();
看看all the things you can do with the Console .
为了好玩,让我们让它更易于重用:
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;
}
然后你会这样调用它:
var inputs = PromptUser("First Name:", "Last Name:", "Badge Number:");
关于在 Console.Readline() 之后的 C# 控制台 Console.WriteLine()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40274601/