Im working with ConsoleKeyInfo in C# but i have problems with Console.ReadKey when I try to write numbers greater than 9 in the console, for example
ConsoleKeyInfo number;
Console.Write("Write a number: ");
number = Console.ReadKey();
If i want to write 10 or 11... the console only reads the "1"
I dont wanna use Console.ReadLine because I don want to press "Enter" for each number.
Is there another way to use Console.ReadKey to wait maybe 1 second before continue?
Thanks
The best you can do is use Console.ReadLine()
. There's no way the program will know you have finished the number.
UPDATE
If you have a fixed length number (ie a 13-digit ISBN), you can use ReadKey, but like this:
string isbn = "";
while (isbn.Length < 13)
{
isbn += Console.ReadKey().KeyChar;
}
As the comments on the question say, Console.ReadKey
only reads a single key by definition. You will need to use a different function if you want to get more input from the console. Try something like this, for instance:
Console.Write("Write a number: ");
string line = Console.ReadLine();
int num = 0;
if (line != null)
num = int.Parse(line);
That's a start, with minimal error checking. See what you can get from there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.