C#连续循环实现求助:基于笑话流程图的程序开发遇阻
Fixing Your C# Joke Loop Logic
Hey there! Let's get that joke loop working properly for you. I took a look at your code and spotted a few issues that are keeping the "hear another joke" functionality from working as expected. Let's break down the problems and fix them step by step.
What's Wrong with the Original Code?
- The
do-whileloop doesn't re-prompt the user to hear another joke after each punchline, so it either runs infinitely or not at all depending on the initial input - You're not generating a new random joke each time the loop runs (it'll just repeat the same joke over and over)
- Input checks are case-sensitive (if the user types "Yes" instead of "yes", it won't register as a valid response)
- There's a small bug in the under-16 message where you print the user's age instead of their name
Here's the Fixed Code
class Program { static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.White; // Get user's basic info Console.WriteLine("What is your name? "); string name = Console.ReadLine(); Console.Clear(); Console.WriteLine("What is your age?"); int age = Convert.ToInt16(Console.ReadLine()); Console.Clear(); string[] jokes = new string[] { "Joke1", "Joke2", "Joke3", "Joke4" }; Random rnd = new Random(); // Check if user is old enough for jokes if (age >= 16) { bool keepListening = true; // First prompt: do they want a joke at all? Console.WriteLine("Do you want to hear a joke?"); string firstChoice = Console.ReadLine().Trim().ToLower(); if (firstChoice != "yes") { Console.WriteLine($"Have a nice day, {name}!"); Console.Read(); return; // Exit early if they don't want a joke } // Main loop for repeating jokes while (keepListening) { // Pick a new random joke each time int randomJokeIndex = rnd.Next(jokes.Length); Console.WriteLine(jokes[randomJokeIndex]); Console.Read(); // Ask if they want another joke Console.WriteLine("Do you want to hear another joke?"); string nextChoice = Console.ReadLine().Trim().ToLower(); // Update loop condition based on their answer keepListening = nextChoice == "yes"; } Console.WriteLine($"Have a nice day, {name}!"); Console.Read(); } else { Console.WriteLine("What a pity! You're too young to hear these jokes!"); Console.Read(); Console.WriteLine($"Have a nice day, {name}!"); Console.Read(); } } }
Key Improvements Explained
- Controlled Loop: We use a boolean variable
keepListeningto clearly control when the joke loop stops. This makes the logic way easier to follow than the original setup. - Fresh Jokes Every Time:
rnd.Next(jokes.Length)generates a new random index each loop iteration, so the user gets a different joke each time they say "yes". - Friendly Input Handling: Using
.Trim().ToLower()on user input ensures that messy inputs like "Yes", "YES", or " yEs " all count as valid "yes" responses. - Fixed Under-16 Bug: We now print the user's name instead of their age in the final message for younger users.
- Cleaner Flow: The initial "want a joke?" check exits early if the user says no, so we don't waste time setting up the loop unnecessarily.
This should give you a smooth, working joke program that lets the user keep hearing new jokes until they decide to stop!
内容的提问来源于stack exchange,提问作者OctaVIve
相关产品推荐
相关产品推荐

