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

C# 题目生成器(开发中):实现用户指定答题数量求助

实现自定义答题数量的C#题目生成器整合方案

嘿,我来帮你搞定这个自定义答题数量的功能!你的现有代码已经搭好了题库和随机出题的基础,只需要添加用户输入验证和题目数量控制的逻辑,就能轻松实现需求。下面是具体的修改步骤和完整代码:

关键修改点

  • 添加用户输入答题数量的交互,并做合法性验证(确保输入是有效数字,且不超过当前题库总数、不小于1)
  • 从题库中随机选取指定数量的题目(避免重复出题)
  • 修正原代码中答题判断逻辑的小bug(原代码不管对错都会输出"你是 incorrect")

修改后的完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

public class Question
{
    public string Text { get; set; }
    public string Answer { get; set; }

    // 原有的IsCorrect方法,假设是这样实现的
    public bool IsCorrect(string userAnswer)
    {
        return string.Equals(userAnswer?.Trim(), Answer?.Trim(), StringComparison.OrdinalIgnoreCase);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var questions = new List<Question>() 
        { 
            new Question { Text = "Q. [Attack On Titan] Which character is the 'Rogue' Titan?", Answer = "Eren"}, 
            new Question { Text = "Q. [Pokemon] Which Pokemon does Ash use mostly?", Answer = "Pikachu" }, 
            new Question { Text = "Q. [Fairy Tail] Who raised Natsu Dragneel when he was a child?", Answer = "Igneel" }, 
            new Question { Text = "Q. [Death Note] What was Light's surname?", Answer = "Yagami" }, 
            new Question { Text = "Q. [Attack On Titan] Who was Eren's best friend?", Answer = "Armin" }, 
            new Question { Text = "Q. [Attack On Titan] Which character is the'Armored' Titan?", Answer = "Reiner" }, 
            new Question { Text = "Q. [Attack On Titan] Which character is the 'Colossal' Titan?", Answer = "Bertholt" }, 
            new Question { Text = "Q. [Death Note] In the series, there was always a shinigami with Light Yagami, what was their name?", Answer = "Ryuk" }, 
            new Question { Text = "Q. [Attack On Titan] Who gave Mikasa their red scarf?", Answer = "Eren" }, 
        }; 

        Random random = new Random(); 
        int correctAnswers = 0;
        int desiredQuestionCount = 0;

        // 第一步:获取并验证用户输入的答题数量
        Console.WriteLine($"当前题库共有 {questions.Count} 道题,请输入你想要的答题数量(1-{questions.Count}):");
        while (!int.TryParse(Console.ReadLine(), out desiredQuestionCount) || desiredQuestionCount < 1 || desiredQuestionCount > questions.Count)
        {
            Console.WriteLine($"输入无效,请输入1到{questions.Count}之间的整数:");
        }

        // 第二步:随机选取指定数量的题目,避免重复
        var selectedQuestions = questions.OrderBy(q => random.Next()).Take(desiredQuestionCount).ToList();

        // 第三步:遍历选中的题目进行答题
        foreach (var question in selectedQuestions) 
        { 
            Console.WriteLine(question.Text); 
            string answer = ""; 
            bool isAnsweredCorrectly = false;

            do 
            { 
                answer = Console.ReadLine(); 
                if (question.IsCorrect(answer)) 
                { 
                    Console.WriteLine($"That is correct! {++correctAnswers}/{desiredQuestionCount}"); 
                    Thread.Sleep(800); 
                    Console.Clear();
                    isAnsweredCorrectly = true;
                } 
                else
                {
                    Console.WriteLine("You are incorrect. Please try again.");
                    Thread.Sleep(400); // 延长一点时间让用户看清提示
                }
            } while (!isAnsweredCorrectly); // 直到答对才进入下一题
        }

        // 答题结束后的统计
        Console.WriteLine($"答题结束!你答对了 {correctAnswers}/{desiredQuestionCount} 道题,正确率 {(double)correctAnswers/desiredQuestionCount*100:F1}%");
        Console.ReadKey();
    }
}

代码说明

  1. 输入验证部分:用int.TryParse确保用户输入的是整数,同时判断数值范围,避免无效输入。
  2. 题目选取逻辑:通过OrderBy(q => random.Next())打乱题库顺序,再用Take(desiredQuestionCount)获取指定数量的题目,保证每道题只出现一次。
  3. 答题逻辑修正:添加了isAnsweredCorrectly标记,只有答错时才提示错误,直到用户答对才进入下一题,同时调整了提示信息和等待时间,提升用户体验。

这样修改后,用户就能自由选择想要答的题目数量,而且代码逻辑更严谨啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:42:41