基于Switch分支语句构建通用答题程序的技术问询
Hey there! Let's walk through how to refine your question-answering program based on the code snippets you shared. First, let's recap your current implementation to make sure we're aligned.
Your current setup defines a multiple-choice question via data.content, then checks if the content contains a target question, and uses a switch statement to return the corresponding answer. Here's your code formatted clearly:
// Define the question content data.content = "1. The word 'virile' means what?\na. Like a rabbit\nb. Like a man\nc. Like a wolf\nd. Like a horse\n"; // Check if content contains the target question and match answer if (data.Content.Contains(question)) { switch (question) { case "The word 'virile' means what?": ans = " Like a man "; break; case "is he a man ?": ans = "No"; break; case "Can you speak ?": ans = "h..."; break; // More cases would go here } }
There are a few pain points in this approach that'll make scaling the program tricky:
- Case & whitespace sensitivity:
Contains()is case-sensitive, so if the input question is lowercase (e.g., "the word 'virile' means what?") or has extra spaces (e.g., "The word 'virile' means what? "), it won't match. - Hardcoded switch statements: Every new question requires modifying the switch block, which gets messy quickly as your question library grows.
- Rigid question matching: Checking if the content contains the question could lead to false matches if a question is a substring of another longer question.
Let's fix these issues with a more flexible, scalable approach:
1. Use a Dictionary for Question-Answer Mapping
Instead of a switch, store questions and answers in a dictionary. This makes adding/removing questions trivial and improves readability. We'll also normalize the text to handle case/whitespace differences.
using System.Text.RegularExpressions; // Create a case-insensitive dictionary to store questions and answers var questionAnswers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { {"The word 'virile' means what?", "Like a man"}, {"is he a man?", "No"}, {"Can you speak?", "h..."} }; // Normalize input question: trim extra spaces and collapse multiple spaces string normalizedQuestion = Regex.Replace(question.Trim(), @"\s+", " "); // Look up the answer if (questionAnswers.TryGetValue(normalizedQuestion, out string answer)) { ans = answer; } else { ans = "Sorry, I don't have an answer for that question yet."; }
2. Parse Multiple-Choice Questions Automatically
Since you're working with multiple-choice questions, you can parse the options from data.content instead of hardcoding the answer. This lets you reuse logic for any multiple-choice question:
// Split the question content into lines string[] lines = data.content.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // Extract the main question (remove the number prefix) string mainQuestion = lines[0].Substring(lines[0].IndexOf(". ") + 2); // Map options (a, b, c, d) to their values var options = new Dictionary<char, string>(); foreach (string line in lines.Skip(1)) { char optionKey = line[0]; string optionValue = line.Substring(line.IndexOf(". ") + 2); options.Add(optionKey, optionValue); } // Store correct option keys for each multiple-choice question var mcQuestionAnswers = new Dictionary<string, char>(StringComparer.OrdinalIgnoreCase) { {"The word 'virile' means what?", 'b'} }; // Get the correct answer text if (mcQuestionAnswers.TryGetValue(mainQuestion, out char correctOption)) { ans = options[correctOption]; // Returns "Like a man" }
- External Question Bank: Store questions/answers in a JSON or CSV file, then load them into the dictionary at runtime. This way you don't need to recompile the program to add new questions.
- Fuzzy Matching: Use a library like
FuzzySharpto handle typos or partial matches (e.g., "What does virile mean?" instead of the exact question). - Question Classification: Add logic to categorize questions (vocabulary, yes/no, etc.) and route them to the appropriate answer handler.
内容的提问来源于stack exchange,提问作者rami_ahmed

