ReadKey等待输入两次后的C#Console.ReadLine(C# Console.ReadLine after ReadKey waits for input two times)

编程入门 行业动态 更新时间:2024-10-22 19:25:53
ReadKey等待输入两次后的C#Console.ReadLine(C# Console.ReadLine after ReadKey waits for input two times)

我不明白我在这里缺少什么,但似乎Console.ReadKey()仍处于活动状态,并且在调用Console.ReadKey()后使用Console.ReadLine()时,控制台让我输入两次。

我已经搜索了如何逃避ReadKey()一旦做出选择,但无济于事。

为了澄清,这是意料之外的行为:当控制台弹出时,用户会看到这三个选项作为示例。 当用户输入“u”或“h”时,控制台不会等待; 它立即执行操作,用户不必按Enter键

我是否做了错误的实现?

static void Main(string[] args) { Console.WriteLine("u up"); Console.WriteLine("h home"); Console.WriteLine("x exit"); Console.WriteLine("---------------------------------"); Console.WriteLine(" [Enter Your Selection]"); Console.WriteLine("---------------------------------"); Console.Write("Enter Selection: "); ConsoleKeyInfo selection; Console.TreatControlCAsInput = true; int value; selection = Console.ReadKey(); if (char.IsDigit(selection.KeyChar)) { value = int.Parse(selection.KeyChar.ToString()); value -= 1; Console.WriteLine("You've entered {0}", value); } else { switch (selection.Key) { case ConsoleKey.U: blurp(); break; case ConsoleKey.H: blurp(); break; case ConsoleKey.X: System.Environment.Exit(0); break; default: Console.WriteLine("Invalid Input..."); break; } } } public static void blurp() { Console.WriteLine(""); Console.Write("Enter Another Value: "); string value = Console.ReadLine(); Console.WriteLine("You've entered {0}", value); }

I don't understand what I am missing here, but it seems that Console.ReadKey() is still active and is causing the Console to make me enter twice when using Console.ReadLine() after invoking Console.ReadKey().

I've searched up and down how to escape ReadKey() once a selection was made, but to no avail.

To clarify, this is the behavior which is unexpected: When the console pops up, the user is presented these three options as an example. When the user then types either "u" or "h", the console does not wait; it immediately performs the action without the user having to press Enter.

Did I do something wrong implementing this?

static void Main(string[] args) { Console.WriteLine("u up"); Console.WriteLine("h home"); Console.WriteLine("x exit"); Console.WriteLine("---------------------------------"); Console.WriteLine(" [Enter Your Selection]"); Console.WriteLine("---------------------------------"); Console.Write("Enter Selection: "); ConsoleKeyInfo selection; Console.TreatControlCAsInput = true; int value; selection = Console.ReadKey(); if (char.IsDigit(selection.KeyChar)) { value = int.Parse(selection.KeyChar.ToString()); value -= 1; Console.WriteLine("You've entered {0}", value); } else { switch (selection.Key) { case ConsoleKey.U: blurp(); break; case ConsoleKey.H: blurp(); break; case ConsoleKey.X: System.Environment.Exit(0); break; default: Console.WriteLine("Invalid Input..."); break; } } } public static void blurp() { Console.WriteLine(""); Console.Write("Enter Another Value: "); string value = Console.ReadLine(); Console.WriteLine("You've entered {0}", value); }

最满意答案

我测试了这段代码并得到了相同的行为:

Console.Write("Enter Selection: "); Console.TreatControlCAsInput = true; ConsoleKeyInfo selection = Console.ReadKey(); if (selection.Key == ConsoleKey.U) { Console.Write("Enter Another Value: "); string valueStr = Console.ReadLine(); Console.WriteLine("You've entered {0}", valueStr); }

解决方案是不使用Console.TreatControlCAsInput = true; 因为这导致了问题。

更多信息,请参阅 Stack Overflow问题TreatControlCAsInput问题。 这是一个错误?

I tested with this code and got the same behavior:

Console.Write("Enter Selection: "); Console.TreatControlCAsInput = true; ConsoleKeyInfo selection = Console.ReadKey(); if (selection.Key == ConsoleKey.U) { Console.Write("Enter Another Value: "); string valueStr = Console.ReadLine(); Console.WriteLine("You've entered {0}", valueStr); }

The solution is to not use Console.TreatControlCAsInput = true; as this is causing the problems.

More information is in Stack Overflow question TreatControlCAsInput issue. Is this a bug?.

更多推荐

本文发布于:2023-08-07 02:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458997.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   Console   ReadKey   ReadLine   times

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!