C# While Loop 不断重复使用第一个用户输入;

C# While Loop keeps reusing the first user input;

正如这里的许多人所说,我是一个非常新的程序员,正在尝试学习基础知识,以便将来掌握一些技能。我正在观看 Bob Tabor 在 C# 上的 channel9 视频,并且学到了很多东西,但是当我开始自己进行时,我发现有些事情我不明白。我正在为一个非常简单的文字游戏编写代码,而且我很清楚我的代码可能是混乱的、冗余的,或者两者兼而有之。

也就是说,它完美地执行了我想要它做的事情,除了一个小问题。如果我一开始就输入是或否,它会继续下去。问题是用户输入错误的选项;我希望他们收到一条消息,说明是或否,然后回到开头再试一次。但是当它执行完,得到消息,然后返回时,它会简单地重新应用第一次的输入,我会陷入无限循环。我知道问题出在哪里,但像我一样缺乏经验,我不确定如何解决问题。我会尝试 copy/paste 我的代码,以便您可以看到我的问题。

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

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

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

while 循环中的 Console.ReadLine 从未分配给任何东西。

改变

 Console.ReadLine();

answer1 = Console.ReadLine(); 

你可能应该在你的主空隙中使用类似这样的东西。这将允许您继续阅读输入,直到用户键入包含 "yes".

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

这可能是一个很好的解决方案,因为一旦您 re-assign 答案的变量,下一个响应就不必是 hard-coded 并且会一直持续到您的客户输入"yes".

一个do-while循环更适合你的情况。

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

只需将您的 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(); 
 }

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

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