Below is a C# Random Quotes Program. This program displays a random quote from a predefined list of quotes each time it is run. It uses the Random class to generate random indices to select quotes from the list.
Full Code
using System;
class RandomQuotesProgram
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Random Quotes Program!");
// Array of quotes
string[] quotes = new string[]
{
"The only limit to our realization of tomorrow is our doubts of today. - Franklin D. Roosevelt",
"In the middle of every difficulty lies opportunity. - Albert Einstein",
"Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
"Do what you can, with what you have, where you are. - Theodore Roosevelt",
"Act as if what you do makes a difference. It does. - William James",
"Life is what happens when you're busy making other plans. - John Lennon",
"The way to get started is to quit talking and begin doing. - Walt Disney",
"Believe you can and you're halfway there. - Theodore Roosevelt",
"You miss 100% of the shots you don’t take. - Wayne Gretzky",
"It always seems impossible until it’s done. - Nelson Mandela"
};
while (true)
{
Console.WriteLine("\nPress 'Enter' to get a random quote, or type 'exit' to quit.");
string userInput = Console.ReadLine();
// Check if the user wants to exit
if (userInput.Trim().ToLower() == "exit")
{
Console.WriteLine("Goodbye!");
break;
}
// Generate a random quote
string randomQuote = GetRandomQuote(quotes);
Console.WriteLine($"\nRandom Quote:\n\"{randomQuote}\"");
}
}
static string GetRandomQuote(string[] quotes)
{
// Generate a random index based on the quotes array length
Random random = new Random();
int index = random.Next(quotes.Length);
return quotes[index];
}
}
Explanation
- Introduction and Quote List:
- The program starts by greeting the user and explaining how to interact with it.
- It defines a string[] array to store the quotes.
string[] quotes = new string[] { "The only limit to our realization of tomorrow is our doubts of today. - Franklin D. Roosevelt", "In the middle of every difficulty lies opportunity. - Albert Einstein", "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill", ... }; - Generating a Random Quote:
- The GetRandomQuote method generates a random index using the Random class.
- The index corresponds to a position in the quotes array, and the quote at that index is returned.
static string GetRandomQuote(string[] quotes) { Random random = new Random(); int index = random.Next(quotes.Length); return quotes[index]; } - User Interaction:
- The program runs in a while (true) loop to allow the user to request multiple quotes.
- The user presses “Enter” to generate a random quote or types “exit” to quit the program.
while (true) { Console.WriteLine("\nPress 'Enter' to get a random quote, or type 'exit' to quit."); string userInput = Console.ReadLine(); if (userInput.Trim().ToLower() == "exit") { Console.WriteLine("Goodbye!"); break; } string randomQuote = GetRandomQuote(quotes); Console.WriteLine($"\nRandom Quote:\n\"{randomQuote}\""); } - Program Exit:
- The loop exits when the user types “exit”, displaying a goodbye message.
if (userInput.Trim().ToLower() == "exit") { Console.WriteLine("Goodbye!"); break; }
Sample Run
Run the program:
Welcome to the Random Quotes Program! Press 'Enter' to get a random quote, or type 'exit' to quit. [User presses Enter] Random Quote: "The only limit to our realization of tomorrow is our doubts of today. - Franklin D. Roosevelt" Press 'Enter' to get a random quote, or type 'exit' to quit. [User presses Enter] Random Quote: "In the middle of every difficulty lies opportunity. - Albert Einstein" Press 'Enter' to get a random quote, or type 'exit' to quit. exit Goodbye!
Features
- Predefined Quote List:
- The program uses a string[] array to store quotes, which can be easily expanded.
- Random Selection:
- The Random class ensures a random quote is selected every time the user presses “Enter.”
- Interactive Loop:
- Allows the user to keep generating quotes until they decide to exit.
- User-Friendly Exit:
- The user can type “exit” at any time to quit the program gracefully.
Ideas
- Add More Quotes:
- Expand the quotes array with more inspiring or humorous quotes.
- Save Quotes to File:
- Save all displayed quotes to a file for the user to review later.
using System.IO; File.AppendAllText("quotes_log.txt", randomQuote + Environment.NewLine); - Categories:
- Organize quotes into categories (e.g., Inspirational, Humorous) and let the user choose a category.
Console.WriteLine("Choose a category: Inspirational, Humorous, etc."); - Graphical Interface:
- Use Windows Forms or WPF to create a graphical version of the program.
- Dynamic Quote Source:
- Fetch quotes from an API like quotable.io or a local database for more variety.
This C# Random Quotes Program is a fun and engaging way to practice working with arrays, loops, and the Random class.