1

I am trying to code a while loop, I am trying to have a user enter a number from 1 to 50 and display the numbers. What I have so far prints out a message if I am within 50 and prints another message if I am over 50 but I need it to continue if the user wants to enter another number but it just exits.

Below is what I have:

class Program
{
    static void Main(string[] args)
    {
        const int from = 1;
        const int to = 50;

        int randomNumber = 50;
        int enteredNumber;

        Console.Write("The number is between 1 and 50.", from, to);
        while (true)
        {
            Console.Write("Enter a number: ");
            if (int.TryParse(Console.ReadLine(), out enteredNumber))
            {
                if (enteredNumber <= randomNumber)
                {
                    Console.WriteLine("You entered a number between 1 & 50");
                    break;
                }
                else
                {
                    if (enteredNumber > randomNumber)
                    {
                        Console.WriteLine("You didn't enter a number between 1 & 50.");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Press any key to exit.");
                        Console.ReadKey();
                    }
                }
            }
        }
    }
}
3
  • 1
    I have no idea what you are asking. Have you stepped through in the debugger? Commented Feb 10, 2014 at 2:08
  • 2
    randomNumber = 50? I feel an XKCD coming on.... xkcd.com/221 Commented Feb 10, 2014 at 2:08
  • 1
    Don't break the while loop and it will continue Commented Feb 10, 2014 at 2:10

3 Answers 3

3

Remove the break it exits the while loop.

Place the break under:

Console.WriteLine("Press any key to exit.");
Console.ReadKey();
Sign up to request clarification or add additional context in comments.

1 Comment

Sure thing :) Could you also accept this as the answer? So your problem is marked as resolved.
1

As others have noted the break is in the wrong location.

Take a look at this. I cleaned it up and added some error handling and now allow the user to quit by entering "Quit" or continue by pressing "Enter".

class Program
{
    private static void Main(string[] args)
    {
        const int from = 1;
        const int to = 50;

        int randomNumber = 50;
        int enteredNumber;

        Console.Write("The number is between 1 and 50.", from, to);
        while (true)
        {
            Console.Write("Enter a number: ");
            if (int.TryParse(Console.ReadLine(), out enteredNumber))
            {
                if (enteredNumber <= randomNumber)
                {
                    Console.WriteLine("You entered a number between 1 & 50. Hit 'Enter' play again or enter 'Quit' to exit");
                    var answer = Console.ReadLine();
                    if (answer.ToLower() == "quit")
                    {
                        break;
                    }
                }
                else if (enteredNumber > randomNumber)
                {
                    Console.WriteLine("You didn't enter a number between 1 & 50. Hit 'Enter' play again or enter 'Quit' to exit");
                    var answer = Console.ReadLine();
                    if (answer.ToLower() == "quit")
                    {
                        break;
                    }

                }
            }
            else
            {
                Console.WriteLine("You didn't enter a number between 1 & 50. Hit 'Enter' play again or enter 'Quit' to exit");
                var answer = Console.ReadLine();
                if (answer.ToLower() == "quit")
                {
                    break;
                }
            }
        }
    }
}

1 Comment

Not a problem. Good luck and happy coding :)
1

In addition to the other answer(s) you can use the continue keyword if you want to exit execution of the current iteration and move to the next iteration:

e.g.

while(true)
{
    if(somecondition)
      continue; // <-- this line will cause execution to move to the next iteration, skipping the code below it

    dosomethingelse();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.