-5

As intended when I type 1, 2, or 3, it is supposed to exit out of the loop or not enter it. But if I write something other than those 3 numbers it is supposed to loop until I type 1, 2, or 3.

But for some reason I can't get it to not enter the loop or to exit it. It doesn't matter what I type even if it is the right number to not enter the loop 1, 2, or 3. It still does enter it and loop regardless.

string userValue = Console.ReadLine();
string message = "";

Console.WriteLine("Card 1, 2, or, 3?");
Console.ReadLine();


while (userValue != "1" || userValue != "2" || userValue != "3")
{
    message = "try again. Card 1, 2, or, 3?";
    Console.WriteLine(message);
    Console.ReadLine();
}

if (userValue == "1")
{
    message = "You win a Coke";
}
else if (userValue == "2")
{
    message = "You win a Diet Coke";
}
else if (userValue == "3")
{
    message = "You win a Apple Juice";
}
Console.WriteLine(message);
Console.ReadLine();
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
FuzzyRedFox
  • 9
  • 1
  • 3
  • @DrewKennedy, why would you use `Equals()`, rather than `==` to compare strings? – David Arno Jan 19 '16 at 22:03
  • @DavidArno In Java that would be the proper way to do it, which I assume is what spawned that comment. Since this is not Java, however, `==` is just fine. – Glorin Oakenfoot Jan 19 '16 at 22:04
  • Before starting a war on `==` versus `Equals()` for string comparison, how about [some light reading](https://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals). The battle is being fought elsewhere... – Frank Bryce Jan 19 '16 at 22:06
  • @GlorinOakenfoot More or less, yeah. In C# they offer the same functionality, with hidden caveats for each idea. For some reason I thought `==` was problematic, which was my Java thinking. – Drew Kennedy Jan 19 '16 at 22:06
  • This is a perfect candidate for solving via debugging. That is what you should do when you have code that runs but doesn't behave as expected. Go line by line and look at what the values are doing. In this case you would have noticed that `userValue` is never being assigned and then you would have noticed that the condition on the while loop is wrong. Debugging is one of the most important skills you will learn as a developer, and you can't always rely on SO to do the debugging for you – Kevin Jan 19 '16 at 22:27
  • Vote to close, typo, the equals and position of Readline inside the loop. – Drew Jan 30 '16 at 19:33

2 Answers2

7

You need to assign the userValue inside your while loop as well.

EDIT: needed to switch your ||s to && as well - the value will ALWAYS not be one of the 3 values ;)

EDIT 2: you also needed to add the assignment before your while loop, as well. Thanks @Nikolai

while (userValue != "1" && userValue != "2" && userValue != "3")
{
    message = "try again. Card 1, 2, or, 3?";
    Console.WriteLine(message);
    // vvvvvv is missing
    userValue = Console.ReadLine();
}

In other news, have you heard of the do..while loop?

Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
6

You need to assign Console.ReadLine() each time, both inside the loop and on the way into it.

userValue = Console.ReadLine();

But you have bigger issues than that, as userValue != "1" || userValue != "2" || userValue != "3" will always be true.

I think you mean userValue != "1" && userValue != "2" && userValue != "3"

I.e.

        string userValue = Console.ReadLine();
        string message = "";

        Console.WriteLine("Card 1, 2, or, 3?");
        userValue = Console.ReadLine(); // Assign

        while (userValue != "1" && userValue != "2" && userValue != "3")
        {
            message = "try again. Card 1, 2, or, 3?";
            Console.WriteLine(message);
            userValue = Console.ReadLine(); // Assign
        }

        if (userValue == "1")
        {
            message = "You win a Coke";
        }
        else if (userValue == "2")
        {
            message = "You win a Diet Coke";
        }
        else if (userValue == "3")
        {
            message = "You win a Apple Juice";
        }
        Console.WriteLine(message);
        Console.ReadLine();

See MSDN on Console.ReadLine for more example usage.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117