忽略输入直到有效响应?

  • 本文关键字:有效 响应 c# .net
  • 更新时间 :
  • 英文 :


抱歉,如果这与现有问题类似 - 我确实尝试过寻找!

无论如何,我有一个程序如下:

static void Main(string[] args)
{
string temp;
Console.WriteLine("question");
temp = Console.ReadLine();
if (temp == "a")
{
Console.WriteLine("Incorrect");
}
if (temp == "b")
{
Console.WriteLine("Incorrect");
}
if (temp == "c")
{
Console.WriteLine("Correct");
}
else
{
Console.WriteLine("Not a valid response");
}
}

我试图让它忽略无效的响应,直到我给出有效的响应。例如,如果我需要按键a,或b,或c但我按下了d,它只会告诉我我的答案不是一个有效的选项,然后等到我给出有效的响应

不确定这是否是你想要的:

public static void Main()
{
Console.WriteLine("question");
bool tryAgain;
do
{
tryAgain = true;
var key = Console.ReadKey();
switch (key.KeyChar)
{
case 'a':
case 'b':
Console.WriteLine();
Console.WriteLine("Incorrect");
tryAgain = false;
break;
case 'c':
Console.WriteLine();
Console.WriteLine("Correct");
tryAgain = false;
break;
default:
Console.Write("b b");
break;
}
} while (tryAgain);
}

如果键入"d"输入,则忽略除有效输入("a"、"b"、"c"(之外的所有输入。

更新此版本与 Enter 一起使用以确认输入:

public static void Main()
{
Console.WriteLine("question");
bool tryAgain;
bool enterPressed;
char input = ' ';
do
{
enterPressed = false;
do
{
tryAgain = true;
var key = Console.ReadKey();
switch (key.KeyChar)
{
case 'a':
case 'b':
case 'c':
input = key.KeyChar;
tryAgain = false;
break;
default:
Console.Write("b b");//Delete last char
break;
}
} while (tryAgain);
var confirmkey = Console.ReadKey();
if (confirmkey.Key == ConsoleKey.Enter)
{
enterPressed = true;
Console.WriteLine();
}
else
{
Console.Write("b b"); //Delete last char
}
} while (!enterPressed);
switch (input)
{
case 'a':
case 'b':
Console.WriteLine("incorrect");
break;
case 'c':
Console.WriteLine("correct");
break;
}
}

你可以实现(提取(一个方法:

private static char AnswerTheQuestion(string question, int answers) {
Console.WriteLine(question);
// Loop until...
while (true) {
// Let's be nice and tolerate leading / trailing spaces
string answer = Console.ReadLine().Trim();
if (answer.Length == 1) {
char result = char.ToLower(answer[0]); // let's ignore case ('A' == 'a' etc.)
if (result >= 'a' && result <= 'a' + answers)
return result; // ...user provide an answer in expected format
}
// Comment it out if you want just to ignore invalid input 
Console.WriteLine($"Sorry, provide an answer in [a..{(char)('a' + answers)}] range");
}
}

然后你可以使用它:

static void Main(string[] args) { 
char result = AnswerTheQuestion(string.Join(Environment.NewLine,
"What is the capital of Russia?",
"  a. Saint-Petersburg",
"  b. Moscow",
"  c. Novgorod"),
3);
Console.WriteLine($"{(result == 'b' ? "correct" : "incorrect")}");
Console.ReadKey(); 
}

这是两个人的教科书案例...while 循环和开关/案例语句。假设"a"仍然是有效的输入:

bool validInput = false;
do{
Console.WriteLine("question");
string input = Console.ReadLine();
switch(input){
case "a":
Console.WriteLine("Incorrect");
validInput = true;
break;
//other cases and default omitted
}
}while(!validInput);
void Main()
{
string temp;
Console.WriteLine("question");
while (true)
{
temp = Console.ReadLine();
if (temp == "a")
Console.WriteLine("Incorrect"); 
else if (temp == "b")
Console.WriteLine("Incorrect");
else if (temp == "c")
Console.WriteLine("Correct");
else if (temp == "exit") // providing a way to exit the loop
break;
else { } // ignore input            
}
}

相关内容

最新更新