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

C#随机字符串问答游戏:指定字符出现次数统计功能实现咨询

C#问答游戏功能实现方案

以下是修复原有问题、并实现「随机选取字符查询出现次数」功能的完整代码:

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

class MainClass
{
    public static void Main(string[] args)
    {
        Random randomgen = new Random();
        int a; 
        string Sa; 
        int b; 
        string sb;
        int m = 0;
        string quit = "QUIT";
        string useranswer;
        int numofquestions;
        int numofquestionsleft;
        int numofcorrect = 0;
        int numoffalse = 0;

        // 询问用户题目总数
        Console.Write("请输入题目总数:");
        numofquestions = Convert.ToInt32(Console.ReadLine());
        numofquestionsleft = numofquestions;

        // 初始化存储题目、用户答案、正确答案的数组
        string[] questionlist = new string[numofquestions];
        string[] useranswerlist = new string[numofquestions];
        string[] rightanswerslist = new string[numofquestions];
        List<string> rightoperationlist = new List<string>();
        List<string> wrongoperationlist = new List<string>();

        // 询问用户生成随机字符串的长度
        Console.WriteLine("请输入3-100之间的数值,作为随机字符串的生成长度:");
        Sa = Console.ReadLine(); 
        a = Int32.Parse(Sa);
        if(a <3 || a>100)
        {
            Console.WriteLine("长度超出范围,程序退出");
            return;
        }

        // 字符集,用于生成随机字符串
        String sarffsa = "A1a2B3b4C5c6D7d8E9eFfGgHhIiKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; 
        while (numofquestionsleft > 0 && m < numofquestions)
        {
            int length = a;
            String randomStr = "";
            for (int i = 0; i < length; i++)
            {
                // 直接用字符集长度作为随机数上限,避免越界
                int alphapet = randomgen.Next(sarffsa.Length);
                randomStr = randomStr + sarffsa.ElementAt(alphapet);
            }

            // 新增逻辑:随机选取目标查询字符,计算正确答案
            char targetChar = randomStr[randomgen.Next(randomStr.Length)];
            int correctCount = randomStr.Count(c => c == targetChar);

            // 提问用户
            Console.WriteLine($"当前随机字符串为:{randomStr}");
            Console.WriteLine($"请问字符【{targetChar}】在上述字符串中的出现次数是多少?输入QUIT可跳过本题");
            useranswer = Console.ReadLine().Trim();

            // 存储题目和答案
            questionlist[m] = randomStr + $" 查询字符:{targetChar}";
            useranswerlist[m] = useranswer;
            rightanswerslist[m] = correctCount.ToString();

            Console.WriteLine("--------------------------------------------------------");
            // 用户输入QUIT跳过本题,记为错误
            if (useranswer.Equals(quit, StringComparison.OrdinalIgnoreCase))
            {
                numofquestionsleft--;
                numoffalse++;
                wrongoperationlist.Add(randomStr);
                m++;
                continue;
            }

            // 校验答案是否正确
            if(int.TryParse(useranswer, out int userResult) && userResult == correctCount)
            {
                Console.WriteLine("回答正确!");
                numofcorrect++;
                rightoperationlist.Add(randomStr);
            }
            else
            {
                Console.WriteLine($"回答错误,正确答案是:{correctCount}");
                numoffalse++;
                wrongoperationlist.Add(randomStr);
            }

            numofquestionsleft--;
            m++;
        }

        // 结果查询菜单
        while (true)
        {
            Console.WriteLine(@"请输入对应数字查询结果:
1 查询正确答题数
2 查询错误答题数
3 查询答对次数最多的字符串模板
4 查询答错次数最多的字符串模板
5 查看所有题目、你的答案和正确答案
输入EXIT退出程序
");
            sb = Console.ReadLine().Trim();
            // 退出逻辑
            if (sb.Equals("EXIT", StringComparison.OrdinalIgnoreCase))
            {
                break;
            }

            if(!int.TryParse(sb, out b))
            {
                Console.WriteLine("输入无效,请输入数字或EXIT");
                continue;
            }

            switch (b)
            {
                case 1:
                    Console.WriteLine("-----------------------------------------------");
                    Console.WriteLine("你一共答对了:" + numofcorrect + " 题");
                    Console.WriteLine("-----------------------------------------------");
                    break;
                case 2:
                    Console.WriteLine("-----------------------------------------------");
                    Console.WriteLine("你一共答错了:" + numoffalse + " 题");
                    Console.WriteLine("-----------------------------------------------");
                    break;
                case 3:
                    if (rightoperationlist.Any())
                    {
                        var result = (from operation in rightoperationlist
                                      group operation by operation into og
                                      orderby og.Count() descending
                                      select og.Key).FirstOrDefault();
                        Console.WriteLine("答对次数最多的字符串模板 = " + result);
                    }
                    else
                    {
                        Console.WriteLine("暂无答对记录");
                    }
                    break;
                case 4:
                    if (wrongoperationlist.Any())
                    {
                        var result02 = (from operation in wrongoperationlist
                                        group operation by operation into og
                                        orderby og.Count() descending
                                        select og.Key).FirstOrDefault();
                        Console.WriteLine("答错次数最多的字符串模板 = " + result02);
                    }
                    else
                    {
                        Console.WriteLine("暂无答错记录");
                    }
                    break;
                case 5:
                    Console.WriteLine("题目\t\t\t你的答案\t正确答案");
                    Console.WriteLine("------------------------------------------------------------------------");
                    for (int z = 0; z < numofquestions; z++)
                    {
                        Console.WriteLine($"{questionlist[z]}\t\t{useranswerlist[z]}\t\t{rightanswerslist[z]}");
                    }
                    Console.WriteLine("------------------------------------------------------------------------");
                    break;
                default:
                    Console.WriteLine("输入的数字无效,请重新输入");
                    break;
            }
        }
        Console.WriteLine("程序已退出,按任意键关闭窗口");
        Console.ReadLine();
    }
}

关键修改说明

  • 补充了原代码缺失的数组、集合定义,修复编译报错问题
  • 新增随机选取查询字符的逻辑:生成随机字符串后,随机从字符串中取一个字符作为查询目标
  • 新增正确答案计算逻辑:用LINQ的Count()方法直接统计目标字符的出现次数,默认大小写敏感,如需忽略大小写可将统计逻辑改为randomStr.Count(c => char.ToLower(c) == char.ToLower(targetChar))
  • 修复原代码重复读取用户输入、输入非数字直接崩溃的问题,增加了输入合法性校验
  • 补充了对错答案的统计逻辑,答对答错会自动更新计数
  • 新增退出功能,输入EXIT可正常退出程序
  • 优化了随机数生成逻辑,直接用字符集长度作为随机数上限,避免数组越界

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 00:27:02