在收到正确的输入之前,请忽略不正确的用户输入

时间:2015-10-03 07:49:43

标签: c# loops while-loop

我的代码出现问题。我必须制作一个简单的选择游戏,我需要拥有它,以便如果用户输入无效选项,它会暂停故事,直到他们选择有效的响应。我试着将整个事情包裹起来(1 == 1)并在控制台窗口中输入无效的响应并打印出来#34;这不是一个选项"无限地。我该如何解决这个问题?感谢。

         // 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;
            }
        }

4 个答案:

答案 0 :(得分:2)

你可以这样做。基本上,你保留一个标志来控制循环。继续它直到您得到正确的输入。一旦得到正确的输入,就通过设置标志来停止循环。

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);

答案 1 :(得分:0)

在检查条件之前,您必须将用户输入重新读入循环中的变量

var input = ReadLine();
while (true) // or some other condition
{
    if (input=="...") { } ...

    input = ReadLine(); // again
}

另外,我在assholeFactor++

时恋爱

答案 2 :(得分:0)

如果问题已得到正确回答,您可以使用标记来检查每个循环。像这样:

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.");
    }
}  

答案 3 :(得分:0)

您可以执行以下操作:

    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);