Home > Mobile >  Noob trying to combine while and if statements
Noob trying to combine while and if statements

Time:04-20

I'm trying to learn C# and wanted to try making a simple program from scratch that asks the user to enter a number between 1-3, every number yields a lottery win and writing "exit" is supposed to exit the program. When one number is answered the user is supposed to be prompted to answer again until choosing to exit. No matter what I do when trying to combine a while loop and if statements I get errors or just infinite loops that won't stop. Probably some simple syntax misunderstanding.. Any help would be greatly appreciated.

This is my code so far:

    static void Main(string[] args)
    {
        string userInput = "";
        Console.Write("Pick a number between 1-3, type 'exit' to stop the program: ");
        userInput = Console.ReadLine();


        while (userInput != "exit")

            if (userInput == "1")
            {
                Console.WriteLine("You won a car");
            }
            else if (userInput == "2")
            {
                Console.WriteLine("You won a boat");
            }
            else if (userInput == "3")
            {
                Console.WriteLine("Sorry, no luck this time. Try again");
            }
            else if (userInput == "exit")
            {
                Console.WriteLine("Exiting...");
                break
            }
            else
            {
                Console.WriteLine("The number has to be between 1-3, try again.");
            }
            Console.ReadLine()
            }


    }

}

}

CodePudding user response:

This should work

static void Main(string[] args)
    {
        string userInput = "";
        Console.Write("Pick a number between 1-3, type 'exit' to stop the program: ");
        userInput = Console.ReadLine();


        while (userInput != "exit")
        {

            if (userInput == "1")
            {
                Console.WriteLine("You won a car");
            }
            else if (userInput == "2")
            {
                Console.WriteLine("You won a boat");
            }
            else if (userInput == "3")
            {
                Console.WriteLine("Sorry, no luck this time. Try again");
            }
            else if (userInput == "exit")
            {
                Console.WriteLine("Exiting...");
                break
            }
            else
            {
                Console.WriteLine("The number has to be between 1-3, try again.");
            }
            userInput = Console.ReadLine()
            }


    }

} 

CodePudding user response:

The value of userInput is never updated in the loop, which means that each time the loop runs, the value stays the same.

A solution is to move the prompt and the reading into the start of the loop.

CodePudding user response:

To add to Zayenz's answer, this is the key to all infinite loops; the circumstance evaluated that decides whether or not to begin the loop is always true. If you ever have this problem again, you just need to look at the code to make sure that whatever should break you out of the loop actually changes whatever criteria it needs to in order to make the condition false;

  • Related