【问题标题】:Disregard incorrect user input until correct input is received忽略不正确的用户输入,直到收到正确的输入
【发布时间】:2015-10-03 07:49:42
【问题描述】:

我的代码有问题。我必须制作一个简单的选择游戏,并且我需要拥有它,这样如果用户输入了一个无效的选项,它就会暂停故事,直到他们选择了一个有效的响应。我试着在一段时间内包装整个东西(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;
            }
        }

【问题讨论】:

  • while 循环?我删除了它,但它在 if(firstChoice) 行之前。
  • 这只是一个简单的错误,你应该测试 firstChoice2,而不是 firstChoice。如果您不选择好的变量名称,那么您将添加错误。
  • 我认为 C# 允许在字符串上使用 switch
  • 确实如此。我喜欢 if 语句:}

标签: c# loops while-loop


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    另外,我在assholeFactor++loled

    【讨论】:

    • 最好让它成为一个do-while,因为无论如何你都需要一个循环周期。
    • 好的,所以我应用了这个建议(我确定是不正确的),但是当我第一次输入无效响应时,它根本没有返回任何消息。我在同一个控制台窗口中再次尝试,它说“这不是一个选项”。当我尝试输入有效回复时,它继续说“这不是一个选项”。
    • 逐步调试代码。我 100% 确定您会发现流程存在缺陷的地方。
    • @khlr 您可以使用do-while,或者您可以将条件变量分配给一个值,该值将被条件循环之前接受并且仅在循环内更新(此处不适用,因为条件只是true),您可以制作一个动作字典,由它们各自的(希望是枚举成员)选择值调用。答案的重点是解释为什么 OP 在无法输入新输入的情况下获得无限次“无效选项”,以及通常如何接近解决方案 - 同时试图 避免 混淆 OP 并压倒他与可能性。
    • 谢谢,我调试了一切,现在可以正常工作了。不过,有一个小问题。我可能会弄明白,但您是否知道为什么第一次出现无效响应时不会提示“这不是一个选项”的消息? IMG 有问题。编辑:jk 我还不能发布图片;-; [链接]imgur.com/9mtf66F
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      你可以这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 2017-02-15
        • 2021-11-19
        • 2021-07-16
        • 1970-01-01
        • 2011-07-17
        相关资源
        最近更新 更多