Home » Magic 8 Ball console application in C#

Magic 8 Ball console application in C#

by wskandot71

Here is a simple Magic 8 Ball console application in C# with a detailed explanation of each part of the code.

This app simulates the classic Magic 8 Ball toy that gives random answers to yes-or-no questions.

What the App Will Do

  • Prompt the user to ask a yes/no question.
  • Randomly select and display a response from a list of 8 Ball-style answers.
  • Repeat this process until the user decides to quit.

Full Code

using System;
using System.Collections.Generic;

class Magic8BallApp
{
    static void Main()
    {
        Console.WriteLine("Welcome to the Magic 8 Ball!");
        Console.WriteLine("Ask any yes/no question, or type 'quit' to exit.");

        // List of Magic 8 Ball responses
        List<string> responses = new List<string>
        {
            "It is certain.",
            "Without a doubt.",
            "You may rely on it.",
            "Ask again later.",
            "Cannot predict now.",
            "Don't count on it.",
            "My sources say no.",
            "Very doubtful."
        };

        Random random = new Random(); // Used to pick a random response

        while (true)
        {
            Console.Write("\nAsk your question: ");
            string question = Console.ReadLine();

            // Check if user wants to quit
            if (string.IsNullOrWhiteSpace(question) || question.ToLower() == "quit")
            {
                Console.WriteLine("Goodbye!");
                break;
            }

            // Simulate thinking
            Console.WriteLine("Shaking the Magic 8 Ball...");
            System.Threading.Thread.Sleep(1000); // Pause for effect

            // Get a random index from the list
            int index = random.Next(responses.Count);
            string answer = responses[index];

            // Display the random answer
            Console.WriteLine("Magic 8 Ball says: " + answer);
        }
    }
}

Detailed Explanation

using System;

Provides access to base classes like Console.

using System.Collections.Generic;

Allows us to use generic collections like List<string>.

class Magic8BallApp

Defines the main class that contains our application.

static void Main()

The entry point of the application. This is where execution starts.

Inside the Main() Method:

1. Greeting the User

Console.WriteLine("Welcome to the Magic 8 Ball!");
Console.WriteLine("Ask any yes/no question, or type 'quit' to exit.");

Introduces the app and explains how to exit.

2. List of Possible Answers

List<string> responses = new List<string>
{
    "It is certain.",
    "Without a doubt.",
    "You may rely on it.",
    "Ask again later.",
    "Cannot predict now.",
    "Don't count on it.",
    "My sources say no.",
    "Very doubtful."
};

This is the core of the app. These are the possible answers the Magic 8 Ball can give.

3. Random Number Generator

Random random = new Random();

The Random object will be used to generate a number to pick a response from the list.

4. Loop to Handle Multiple Questions

while (true)
{
    // input, logic, output
}

The program runs in a loop until the user types quit.

5. Reading the User’s Question

string question = Console.ReadLine();

Takes input from the user.

6. Exit Condition

if (string.IsNullOrWhiteSpace(question) || question.ToLower() == "quit")

If the user types nothing or enters “quit”, the app ends.

7. Delay to Simulate Shaking

System.Threading.Thread.Sleep(1000);

Adds a one-second pause to make it feel more interactive.

8. Choosing a Random Answer

int index = random.Next(responses.Count);
string answer = responses[index];

Randomly selects one of the responses from the list.

9. Displaying the Answer

Console.WriteLine("Magic 8 Ball says: " + answer);

Shows the selected answer to the user.

How to Run This App

  1. Open your C# IDE (Visual Studio, VS Code, etc.).
  2. Create a new Console App project.
  3. Replace the content of Program.cs with the code above.
  4. Build and run the project.

Optional Ideas

  • Add more responses for variety.
  • Log the questions and answers.
  • Change text colors for fun.
  • Add input validation for invalid inputs.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.