Stuck in an infinite if loop

I am working on a C # sim console lab for my class in Visual Studio 2015. I broke my program into three if loops depending on the user's response. I have a dedicated dedicated while loop if the user does not enter the correct answer. For some reason, I'm stuck in this loop and can't get out. In addition, since it is to tell the user to enter a valid answer, this is the first if statement. Because of this, even if I entered "y", "Y", "n" or "N", it is still initialized. Here is the part in question.

        // get response from user
        response = ReadLine();

        // loop starts if user does not reply with valid response in order to retrieve a valid response
        if (response != "N" || response != "Y" || response != "n" || response != "y")
        {
            do
            {
                WriteLine("Please reply with Y or N");
                response = ReadLine();
            }
            while (response != "N" || response != "Y" || response != "n" || response != "y");
        }

I use the operator or, therefore, I do not understand why it loops as it is.

+4
source share
5 answers
response != "N" || response != "Y" || response != "n" || response != "y"

response != "N" && response != "Y" && response != "n" && response != "y"

,

+2

&& ||

response != "N" && response != "Y" && response != "n" && response != "y"
+1

, && ||; if do..while while..do:

response = Console.ReadLine();

while (response != "N" && response != "Y" && response != "n" && response != "y") {
  WriteLine("Please reply with Y or N");
  response = Console.ReadLine();
}

- :

  Dictionary<String, Boolean> expectedResponses = new Dictionary<String, Boolean>() {
    {"Y", true},
    {"y", true},
    {"N", false},
    {"n", false},
  };

  ...

  response = Console.ReadLine();

  while (!expectedResponses.ContainsKey(response)) {
    WriteLine("Please reply with Y or N");
    response = Console.ReadLine();
  }
  ...
  if (expectedResponses[response]) {
    // user said "yes"
  }
  else {
    // user said "no"
  } 
+1

, && ||.

    response = ReadLine();

    // loop starts if user does not reply with valid response in order to retrieve a valid response
    if (response != "N" && response != "Y" && response != "n" && response != "y")
    {
        do
        {
            WriteLine("Please reply with Y or N");
            response = ReadLine();
        }
        while (response != "N" && response != "Y" && response != "n" && response != "y");
    }
0

response != "N" || response != "Y" || response != "n" || response != "y"

DeMorgan . != ==. || &&. a ! .

!(response == "N" && response == "Y" && response == "n" && response == "y")

Now you can see that the response must be equal to Nand Yand Nand Yat the same time. Thus, his always false assertion and denial before he makes it always true.

So, now you understand that you must put OR instead of AND. then use the law of the demon to simplify again.

!(response == "N" || response == "Y" || response == "n" || response == "y") <=>
response != "N" && response != "Y" && response != "n" && response != "y"
0
source

Source: https://habr.com/ru/post/1613780/


All Articles