代码之家  ›  专栏  ›  技术社区  ›  vidathan

C#While循环继续重用第一个用户输入;

  •  1
  • vidathan  · 技术社区  · 9 年前

    正如在座的许多人所说,我是一名非常新的程序员,我试图学习基础知识,以便在未来掌握一些技能。我正在观看Bob Tabor的C#频道9视频,我学到了很多东西,但当我开始自己学习时,我发现有些东西我不懂。我正在为一个非常简单的文本游戏编写代码,我很清楚我的代码可能是杂乱的,或者是多余的,或者两者兼而有之。

    尽管如此,它还是完美地执行了我想要它做的事情,只留下一个小嗝。如果我在开头输入“是”或“否”,它会继续进行得很好。问题是,如果用户输入了不正确的内容,该选项;我希望他们收到一条信息,说明是或否,然后回到开始,再试一次。但是当它执行完,得到消息,然后返回时,它只会从第一次重新应用输入,我会陷入无限循环。我知道问题出在哪里,但由于我缺乏经验,我不确定如何解决问题。我将尝试复制/粘贴我的代码,以便您看到我的问题。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FirstSoloAttempt
    {
      class Program
      {
        static void Main(string[] args)
        {
          bool incorrectAnswer = true; //if user inputs anything other than yes or no, reloop to beginning
          Console.WriteLine("Do you want to play a game?");
    
          string answer1 = Console.ReadLine(); //somehow, need to allow this input to be changed second time
    
          while (incorrectAnswer)
          {
            if (answer1.Contains("yes"))
            {
              incorrectAnswer = false;
              Console.WriteLine("Great! Let's begin. Which door is the new car behind? It is behind door 1, door 2, or door 3?");
              string answer2 = Console.ReadLine();
    
              if (answer2.Contains("door 1"))
              {
                Console.WriteLine(" Your new car is a new 2016 Chevy Corvette! Congratulations!");
                Console.WriteLine("Are you satisfied with your new car?");
    
                string answer3 = Console.ReadLine();
    
                if (answer3.Contains("yes"))
                {
                  Console.WriteLine("Fantastic!");
                }
                else
                {
                  Console.WriteLine("I'm sorry you are not satisfied. You may return it for a different car.");
                }
              }
              else
              {
                Console.WriteLine("Oh! Bummer! You didn't win. Thanks for playing!");
              }
            }
            else if (answer1.Contains("no"))
            {
              incorrectAnswer = false;
              Console.WriteLine("Alright then! Goodbye!");
            }
            else
            {
              Console.WriteLine("I'm sorry, I didn't understand that. Please answer with yes or no.");
            }
    
            Console.ReadLine();
          }
        }
      }
    }
    

    基本上,我的代码不允许我接收新的输入以更改程序循环的结果。我知道这与从第一个字符串answer1读取输入的代码有关,所以我如何才能为接下来的所有尝试更改它?

    感谢您的帮助!对我来说,编码很快就变得有趣了,其他人的帮助社区是一个很大的积极因素。

    5 回复  |  直到 9 年前
        1
  •  0
  •   Gilgamesh    9 年前

    你的 Console.ReadLine while循环内部从未分配给任何内容。

    改变

     Console.ReadLine();
    

    answer1 = Console.ReadLine(); 
    
        2
  •  0
  •   Greg    9 年前

    只需将更改else语句添加到下面

    else
    {
          Console.WriteLine("I'm sorry, I didn't understand that. Please answer with yes or no.");
          Console.WriteLine("Do you want to play a game?");
    
          answer1 = Console.ReadLine(); 
     }
    
        3
  •  0
  •   Igor Monteiro    9 年前

    后者 Console.ReadLine() 不更改第一个变量的状态

    更改为 var answer1 = Console.ReadLine();

        4
  •  0
  •   Bink    9 年前

    你可能应该在你的主要空间里使用这样的东西。这将允许您继续阅读输入,直到用户键入包含“是”的内容。

    string answer1 = Console.ReadLine();
    while (answer1.Contains("yes") != true)
    {
        answer1 = Console.ReadLine();
    }
    

    这可能是一个很好的解决方案,因为一旦您为答案重新分配了变量,它就不必为下一个响应进行硬编码,并且会一直持续到您的客户键入“是”。

        5
  •  0
  •   Ion Sapoval    9 年前

    do-while循环更适合您的情况。

    do
      {
        string answer1 = Console.ReadLine();
        //your existing code from while loop ...
      }while(incorrectAnswer)