简体   繁体   中英

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. 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?

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.

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
  {
    string answer1 = Console.ReadLine();
    //your existing code from while loop ...
  }while(incorrectAnswer)

Just add change your else statement into the following

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

change to var answer1 = Console.ReadLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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