c# - 有没有办法输入数字而不立即显示它们?

标签 c# .net console-application

我想输入 10 个数字,然后像这样在 1 行中显示它们: 1,4,5,2,456,23,... 等等..

它在我输入它们时一直在写它们,最后当它应该在 1 行中显示所有数字时它只显示最后一个。

我知道使用随机数是可能的,但是当我自己输入它们时,我不知道如何完全不显示它们或将它们保持在一行中,甚至可能吗?

int a;
int x;
Console.WriteLine("a:");
a = int.Parse(Console.ReadLine());
x = 10;
for (int i = 0; i < x; i++)
{
    a = int.Parse(Console.ReadLine());
}
Console.ReadKey();

最佳答案

你可以使用

Console.ReadKey(true)

它从控制台读取 key 但不显示它。

您可以使用它从控制台读取单词而不显示它

public static string ReadHiddenFromConsole()
{
    var word = new StringBuilder();
    while (true)
    {
        var i = Console.ReadKey(true);
        if (i.Key == ConsoleKey.Enter)
        {
            Console.WriteLine();
            break;
        }
        if (i.Key == ConsoleKey.Backspace)
        {
            if (word.Length > 0)
            {
                word.Remove(word.Length - 1, 1);
                Console.Write("\b \b");
            }
        }
        else
        {
            word.Append(i.KeyChar);
            Console.Write("*");
        }
    }
    return word.ToString();
}

关于c# - 有没有办法输入数字而不立即显示它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26783640/

相关文章:

c++ - QT控制台中的屏幕如何清晰?

.net - Sub Main() 中通过调用自身无限循环?

c# - 结构的快速序列化/反序列化

c# - 无法使用 Linq 插入到 SQL Server Always Encrypted 表中

c# - 当游戏中的CPU使用率达到峰值时,为什么我的NAudio音乐会卡住?

c# - 调用结果对等待任务的影响

pip - 脆弱的控制台脚本 : pkg_resources. DistributionNotFound

c# - 如何使用 ionic 库压缩文件

c# - 使用反射创建通用委托(delegate)

c# - DateTime with Hijri Calender - 30/02 总是抛出异常