How would I be able to make the program stop with only a specific input in C#

Viewed 66

I am very new to C# and I have just created my first program, which is your classic "number guesser".

In my program, after the user has guessed the correct number, I want to give them an option where they can either type "Y" or "N" to continue or end the game.

Where my issue lies is, if the user were to type in the letter "G" at this stage, the program would continue and ask the user to input another number.

How do I enable my code to keep looping at this stage until either Y is pressed to continue the game or N is pressed to end the program?

using System;

// Namespace
namespace NumberGuesser
{
    // Main Class
    class Program
    {
        // Entry Point Method
        static void Main(string[] args)
        {
            GetAppInfo();   // Run GetAppInfo function to get info

            GreetUser();   // Ask for user's name and greet

            while (true)
            {
                // Create a new Random object
                Random random = new Random();

                // Initial correct number
                int correctNumber = random.Next(1, 11);

                // Initial guess var
                int guess = 0;

                // Ask user for number
                Console.WriteLine("Guess a number between 1 and 10:");

                // While guess is not correct
                while (guess != correctNumber)
                {
                    // Get users input
                    string input = Console.ReadLine();

                    // Make sure it's a number
                    if (!int.TryParse(input, out guess))
                    {
                        // Print error message
                        PrintColourMessage(ConsoleColor.Red, "Please user an actual number.");

                        // Keep going
                        continue;
 
                    }

                    // Make sure the number guessed is between 1 - 10
                    if (guess > 10)
                    {
                        // Print error message
                        PrintColourMessage(ConsoleColor.Red, "Please enter a number from 1 to 10.");

                        // Keep going
                        continue;
                    }

                    // Cast to int and put in guess
                    guess = Int32.Parse(input);

                    // Match guess to correct number
                    if (guess != correctNumber)
                    {
                        // Print error message
                        PrintColourMessage(ConsoleColor.Red, "Wrong number, please try again.");

                    }

                }

                // Print success message
                PrintColourMessage(ConsoleColor.Yellow, "You are CORRECT!!!");

                // Ask to play again
                Console.WriteLine("Play again? [Y or N]");

                // Get answer
                string answer = Console.ReadLine().ToUpper();

                if (answer == "Y")
                {
                    continue;
                }

                else if (answer == "N")
                {
                    return;
                }

                else
                {
                    return;
                }

               


            }
        }

        // Get and display app info
        static void GetAppInfo()
        {
            // Set app vars
            string appName = "Number Guesser";
            string appVersion = "1.0.0";
            string appAuthor = "Jack Thomas";

            // Change text colour
            Console.ForegroundColor = ConsoleColor.Green;

            // Write out app info
            Console.WriteLine("{0}: Version {1} by {2}", appName, appVersion, appAuthor);

            // Reset text colour
            Console.ResetColor();
        }

        // Ask user's name and greet
        static void GreetUser()
        {
            // Ask users name
            Console.WriteLine("What is your name?");

            // Get user input
            string inputName = Console.ReadLine();

            Console.WriteLine("Hello {0}, let's play a game...", inputName);
        }

        // Print colour message
        static void PrintColourMessage(ConsoleColor color, string message)
        {
            // Change text colour
            Console.ForegroundColor = color;

            // Prints message
            Console.WriteLine(message);

            // Reset text colour
            Console.ResetColor();
        }

    }

}

1 Answers

Just instead of doing this:

// Ask to play again
Console.WriteLine("Play again? [Y or N]");

// Get answer
string answer = Console.ReadLine().ToUpper();

if (answer == "Y")
{
   continue;
}
else if (answer == "N")
{
   return;
}

do that:

// Ask to play again
Console.WriteLine("Play again? [Y or N]");

bool toContinue = false;
bool invalidResponse = true;

while(invalidResponse) {
   // Get answer
   string answer = Console.ReadLine().ToUpper();

   if (answer == "Y")
   {
     toContinue = true;
     invalidResponse = false;
   }
   else if (answer == "N")
   {
      invalidResponse = false;
   }
}

if(toContinue != true) return;

I've created while loop wich loops while invalidResponse variable is true, invalidResponse is true when we not entered any of these characters "Y" or "N". You can also make Console.WriteLine when user enters invalid response.

If user does not entered "Y" it sets toContinue to false and it ends program.

Related