在 Console.Readline() 之后的 C# 控制台 Console.WriteLine()?

标签 c# console

我希望控制台输出基本上具有表单的一般外观。

这是我希望输出的样子的表示:

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/

相关文章:

c# - NPoco:为什么我的 Delete<Model>() 调用会抛出 NullReferenceException?

emacs 控制台模式 Org-mode 删除线未按预期显示

unix - 控制台脚本使用配置文件的 Unix 方式是什么?

C# 控制台 - 如何在没有新行的情况下读取 ReadLine()? (或撤消 a\n 或垂直\b)

python - 如何获取 tkinter 文本小部件中最后一行的内容(Python 3)

c# - 如何将 facebook Graph 日期时间转换为 c# DateTime

c# - 使用 DirectoryEntry 对象调用 ChangePassword

c# - 绑定(bind)和级联下拉列表 mvc 3

javascript - 用户可以使用控制台在我的网站上发送无效投注

c# - 识别网络路径存在的最快方法