You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#的ReadLine中仅接受合法选项(驾照考试项目场景)

解决方案:整合输入校验到驾照考试项目

核心思路是把你之前的do-while校验逻辑放在Program.cs中,强制用户输入合法的a/b/c选项后,再赋值给Questions对象的UserAnswer属性。以下是修改后的完整代码:

1. 修改Program.cs

namespace DrivingLicenseExam
{
    class Program
    {
        static void Main(string[] args)
        {
            // 先声明分数变量,避免编译报错
            int totalScore = 0;
            Questions firstQuestion = new Questions("1. You may drive off the paved roadway to pass another vehicle: ", "c", "");

            // 打印题目选项
            Console.WriteLine(firstQuestion.question);
            Console.WriteLine("----");
            Console.WriteLine("a) - If the shoulder is wide enough to accommodate your vehicle.");
            Console.WriteLine("b) - If the vehicle ahead of you is turning left.");
            Console.WriteLine("c) - Under no circumstances.");

            // 使用do-while循环校验输入
            string userInput;
            do
            {
                Console.Write("Your answer is: ");
                userInput = Console.ReadLine().ToLower();
                // 校验输入合法性
                if (userInput != "a" && userInput != "b" && userInput != "c")
                {
                    Console.WriteLine("Invalid answer. Please choose between \"a\", \"b\" or \"c\".");
                }
            } while (userInput != "a" && userInput != "b" && userInput != "c");

            // 输入合法后再赋值给UserAnswer
            firstQuestion.UserAnswer = userInput;

            // 判断答案是否正确
            if (firstQuestion.UserAnswer == firstQuestion.correctAnswer)
            {
                totalScore++;
                Console.WriteLine("Correct!");
            }
            else
            {
                Console.WriteLine($"Wrong answer. Correct answer is {firstQuestion.correctAnswer}.");
            }
        }
    }
}

2. 简化Questions.cs

因为输入已经在外部完成严格校验,UserAnswer的setter可以简化,避免重复提示:

namespace DrivingLicenseExam
{
    class Questions
    {
        public string question;
        public string correctAnswer;
        private string userAnswer;

        public Questions(string aQuestion, string aCorrectAnswer, string aUserAnswer)
        {
            question = aQuestion;
            correctAnswer = aCorrectAnswer;
            UserAnswer = aUserAnswer;
        }

        public string UserAnswer
        {  
           get { return userAnswer; } 
           set {
                // 外部已校验,直接赋值即可
                userAnswer = value;
           }
        }
    }
}

可选优化:封装校验方法(适配多题目场景)

如果后续要添加更多题目,可以把校验逻辑封装成静态方法,避免重复代码:

namespace DrivingLicenseExam
{
    class Program
    {
        static void Main(string[] args)
        {
            int totalScore = 0;
            Questions firstQuestion = new Questions("1. You may drive off the paved roadway to pass another vehicle: ", "c", "");

            // 打印题目...
            string userInput = GetValidAnswer();
            firstQuestion.UserAnswer = userInput;
            // 判断答案...
        }

        // 封装通用的输入校验方法
        static string GetValidAnswer()
        {
            string input;
            do
            {
                Console.Write("Your answer is: ");
                input = Console.ReadLine().ToLower();
                if (!new[] { "a", "b", "c" }.Contains(input))
                {
                    Console.WriteLine("Invalid answer. Please choose between \"a\", \"b\" or \"c\".");
                }
            } while (!new[] { "a", "b", "c" }.Contains(input));
            return input;
        }
    }
}

内容的提问来源于stack exchange,提问作者South

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:35:15