C#console console.WriteLine()在console.Readline()之后?
我希望控制台输出基本上具有表单的一般外观 以下是我希望输出的外观表示:C#console console.WriteLine()在console.Readline()之后?,c#,console,C#,Console,我希望控制台输出基本上具有表单的一般外观 以下是我希望输出的外观表示: First Name: //using Console.ReadLine() right here while there is other text below Last Name: Badge Number: 然后 最后 First name: Joe Last name: Blow Badge Number: //using Console.ReadLine() right here 你需要这个方法 看一看 为
First Name: //using Console.ReadLine() right here while there is other text below
Last Name:
Badge Number:
然后
最后
First name: Joe
Last name: Blow
Badge Number: //using Console.ReadLine() right here
你需要这个方法
看一看
为了好玩,让我们让它更具可重用性:
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;
}
显示您正在使用的代码,然后告诉您面临的困难是什么?我想您需要通过手动移动光标。我不知道有什么简单的方法可以做到这一点。使用
SetCursorPosition(int left,int top)
将光标放在控制台上,Link:当使用WPF或WinForms实际创建一个表单要容易得多的时候,为什么要尝试将控制台用作表单?当用户越过姓氏,然后意识到他们在名字上犯了错误时会发生什么?您希望他们如何回去更正它?注意:您可以使用ICollection
而不是IEnumerable
,并使用接口.Count
属性立即获取i
。这将有助于在编写所有提示后,在开始时引发过多提示的异常。@ScottChamberlain是的,但IEnumerable是“特殊的”,因为这意味着您可以将其直接用于linq运算符。例如:[“名字”、“姓氏”、“徽章号码”]。选择(s=>s+:”)
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();
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:");