简体   繁体   English

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

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

As many here have said, I am a very new programmer, attempting to learn the basics so that I can have some skills under my belt for the future. 正如这里许多人所说的,我是一个非常新的程序员,试图学习基础知识,以便将来拥有一些技能。 I am watching the channel9 videos on C# by Bob Tabor, and am learning a ton, but when I started to proceed on my own, I have discovered there are some things I don't understand. 我正在观看Bob Tabor在C#上的Channel9视频,并且学到了很多东西,但是当我开始自己进行创作时,我发现有些事情我不理解。 I am writing code for a very simple text game, and I am well aware my code may be messy, or redundant, or both. 我正在为一个非常简单的文字游戏编写代码,并且我很清楚我的代码可能是混乱的,冗余的,或两者兼而有之。

That being said, it executes flawlessly for what I wanted it to do, save one minor hiccup. 话虽如此,它可以完美执行我想要的工作,省去了一个小小的麻烦。 If I input in the beginning Yes or no, it continues well enough. 如果我以“是”或“否”开头输入,则它会继续进行。 The problem is the option for if the user inputs something incorrect; 问题是用户是否输入了错误的选项; I want them to receive a message stating to say yes or no, then to go back to the beginning and try again. 我希望他们收到一条消息,说是或否,然后回到开头再试一次。 But when it executes through, gets that message, and goes back, it will simply reapply the input from the first time, and I'll get stuck in an infinite loop. 但是,当它执行完毕,获取该消息并返回时,它将仅从第一时间重新应用输入,而我将陷入无限循环。 I know where the problem is, but being as inexperienced as I am, I am not sure how to fix the problem. 我知道问题出在哪里,但是由于我没有经验,所以我不确定如何解决该问题。 I will try to copy/paste my code so you can see my problem. 我将尝试复制/粘贴我的代码,以便您可以看到我的问题。

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

Basically, my code won't allow me to receive new input in order to change the result of the program loop. 基本上,我的代码不允许我接收新输入以更改程序循环的结果。 I know ti has to do with the code reading the input from the first string, answer1, so how can I change it for all following attempts? 我知道ti与从第一个字符串answer1读取输入的代码有关,那么如何为以后的所有尝试更改它?

Thanks for any help in advance! 感谢您的任何帮助! Coding is quickly becoming interesting to me, and the community of others helping out is a big positive. 编码对我而言很快变得很有趣,而其他人提供帮助的社区也带来了很大的积极影响。

your Console.ReadLine inside th while loop is never assigned to anything. while循环内的Console.ReadLine永远不会分配给任何东西。

Change 更改

 Console.ReadLine();

To

answer1 = Console.ReadLine(); 

You should probably use something like this within your main void. 您可能应该在您的主要空白中使用类似的内容。 This would allow you to keep reading the input until the user types something containing "yes". 这将使您能够继续阅读输入内容,直到用户键入包含“是”的内容为止。

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

This would probably be a good solution because once you re-assign the variable for the answer, it wouldn't have to be hard-coded for the next response and would continue until your client has typed "yes". 这可能是一个很好的解决方案,因为一旦为答案重新分配了变量,就不必为下一个响应进行硬编码,并且会一直持续到您的客户键入“是”。

A do-while loop is more suitable for your case. do-while循环更适合您的情况。

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

Just add change your else statement into the following 只需将更改您的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(); 
 }

The latter Console.ReadLine() is not changing the state of the first variable 后一个Console.ReadLine()不会更改第一个变量的状态

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

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

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