[英]C# Console.ReadKey read numbers greater than 9
我在C#中使用ConsoleKeyInfo工作,但是當我嘗試在控制台中寫入大於9的數字時,我遇到了Console.ReadKey問題。
ConsoleKeyInfo number;
Console.Write("Write a number: ");
number = Console.ReadKey();
如果我想寫10或11 ...控制台僅讀取“ 1”
我不想使用Console.ReadLine,因為我不想為每個數字按下“ Enter”。
還有另一種方法可以使用Console.ReadKey等待1秒鍾再繼續嗎?
謝謝
最好的辦法是使用Console.ReadLine()
。 程序無法知道您已經完成了該號碼。
UPDATE
如果您有固定長度的數字(即13位ISBN),則可以使用ReadKey,但是像這樣:
string isbn = "";
while (isbn.Length < 13)
{
isbn += Console.ReadKey().KeyChar;
}
就像對問題的評論所說, Console.ReadKey
僅按定義讀取單個鍵 。 如果要從控制台獲取更多輸入,則需要使用其他功能。 嘗試這樣的事情,例如:
Console.Write("Write a number: ");
string line = Console.ReadLine();
int num = 0;
if (line != null)
num = int.Parse(line);
這是一個開始,只需最少的錯誤檢查。 看看您能從那里得到什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.