我試圖要求用戶僅按“ ”鍵,如果用戶輸入錯誤,請告訴他這不是操作;例如,如果用戶輸入數字或任何其他鍵,告訴用戶“你必須輸入一個操作”我嘗試了下面的代碼但沒有得到修復!
Console.WriteLine("press ' ' to add\npress '-' to delete");
//these are the ways i have tried:
//1:
char add = ' ';
if (Console.ReadKey() == add) ;
//2:
string input = Console.ReadLine();
char inputChar = Convert.ToChar(input);
//if user give number the program shows error to me
if (inputChar == add) ;
//3:
if (Console.ReadKey().Key == ConsoleKey. ) ;
uj5u.com熱心網友回復:
您可以嘗試實作do .. while回圈:
char op;
do {
Console.WriteLine("press ' ' to add\npress '-' to delete");
op = Console.ReadKey().KeyChar;
}
while (op != ' ' && op != '-');
我們一直詢問用戶,直到提供有效的操作(op)(' '或者'-')
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408707.html
標籤:
