繁体   English   中英

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

[英]C# While Loop keeps reusing the first user input;

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

话虽如此,它可以完美执行我想要的工作,省去了一个小小的麻烦。 如果我以“是”或“否”开头输入,则它会继续进行。 问题是用户是否输入了错误的选项; 我希望他们收到一条消息,说是或否,然后回到开头再试一次。 但是,当它执行完毕,获取该消息并返回时,它将仅从第一时间重新应用输入,而我将陷入无限循环。 我知道问题出在哪里,但是由于我没有经验,所以我不确定如何解决该问题。 我将尝试复制/粘贴我的代码,以便您可以看到我的问题。

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

您可能应该在您的主要空白中使用类似的内容。 这将使您能够继续阅读输入内容,直到用户键入包含“是”的内容为止。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM