忽略不正确的用户输入,直到收到正确的输入
本文关键字:输入 用户 不正确 | 更新日期: 2023-09-27 18:15:43
我的代码有问题。我必须制作一款简单的选择游戏,如果用户输入了一个无效的选项,它就会暂停故事,直到他们选择一个有效的回应。我尝试在一段时间内包装整个事情(1==1)并在控制台窗口中输入无效响应,它无限地打印出"这不是一个选项"。我该如何补救??谢谢。
// First choice, scene 1 if-statements
while (1 == 1)
{
String firstScene = Console.ReadLine();
firstScene = firstScene.Trim();
String firstChoice = firstScene.ToLower();
Console.WriteLine();
if (firstChoice == "rudely")
{
assholeFactor++;
Console.WriteLine(">>'"You're damn right it was! We've been working on this for years. I thought you'd be happy that all those hours at the office actually amounted to something.'" (Douche factor increased)");
break;
}
if (firstChoice == "nicely")
{
niceFactor++;
Console.WriteLine(">>'"No, it wasn't silly. I can see where you're coming from. I suspect there will be a lot of people with those same type of questions upon the release of the first model.'" (Nice factor increased)");
break;
}
if (firstChoice == "silence")
{
judgement--;
Console.WriteLine(">>You sip your wine and say nothing. (Judgement decreased)");
break;
}
if (firstChoice != "rudely" || firstChoice != "nicely" || firstChoice != "silence")
{
Console.WriteLine("That wasn't an option.");
continue;
}
}
您可以这样做。基本上,你保留一个标志来控制循环。继续这样做,直到得到正确的输入。一旦得到正确的输入,通过设置标志停止循环。
string firstChoice = "getInputFromUser....";
var isCorrectInput = false;
do
{
if (firstChoice == "rudely")
{
isCorrectInput = true; //stop further loop iteration
assholeFactor++;
.....
}
else if
... //set isCorrectInput as well
else
{
//if input didn't match options, continue loop
Console.WriteLine("That wasn't an option. Enter again...");
firstChoice = Console.ReadneLine(); //
}
} while(!isCorrectInput);
必须在循环中重新读取用户输入到变量中的内容,然后再检查它的条件:
var input = ReadLine();
while (true) // or some other condition
{
if (input=="...") { } ...
input = ReadLine(); // again
}
还有,我看了看assholeFactor++
您可以在每个循环中使用一个标志来检查问题是否正确回答。像这样:
var answered = false;
while(!answered) // Loop until the question has been correctly anwered
{
var firstChoice = ReadLine();
if (firstCoice == "rudely")
{
assholeFactor++;
Console.WriteLine(">>'"You're damn right it was! We've been working on this for years. I thought you'd be happy that all those hours at the office actually amounted to something.'" (Douche factor increased)");
answered = true; // Question answered --> shouldn't keep asking
}
else if...
else
{
Console.WriteLine("That wasn't an option.");
}
}
你可以这样做:
Dictionary<string,Tuple<string,Action>> Choices = new Dictionary<string, Tuple<string, Action>>();
int assholeFactor = 0;
int niceFactor = 0;
int judgement = 0;
Choices.Add("rudely" , new Tuple<string, Action>("You're damn right it w.." , () => assholeFactor++ ));
Choices.Add("nicely" , new Tuple<string, Action>("No, it wasn't silly. I ca.." , () => niceFactor++ ));
Choices.Add("silence", new Tuple<string, Action>("You sip your wine and say nothing.", () => judgement-- ));
do
{
string option = Console.ReadLine();
if (Choices.ContainsKey(option))
{
Console.Out.WriteLine(Choices[option].Item1);
Choices[option].Item2();
break;
}
Console.WriteLine("That wasn't an option.");
} while (true);