【问题标题】:how many time a char have been repeted in a random string一个字符在随机字符串中重复了多少次
【发布时间】:2021-12-25 18:52:48
【问题描述】:

我有这个代码,这是一个简单的游戏,你选择多少个问题和你想要多少个字符,程序会生成一个随机的字符和数字字符串,但是我如何在每个问题中选择一个随机字符并询问用户有多少例如,这个字符被重复的时间 char A 在此字符串中重复了多少次:iasfhAjfalkjA 答案 = 2 次​​strong>

我不知道我是否应该把整个代码,但我认为这是我的代码有帮助

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

            //ASKING THE USER FOR THE MAX NUMBER OF QUESTIONS WANT TO BE ASKED
            Console.Write("Max Question : ");
            numofquestions = Convert.ToInt32(Console.ReadLine());
            numofquestionsleft = numofquestions;
            //ASKING THE USER TO PUT A NUMBER BETWEEN 3 AND 100 THAT WILL HELP GENERATE A RANDOM string


            Console.WriteLine("enter a value between 3 and 100");
            Sa = Console.ReadLine(); a = Int32.Parse(Sa);

//here is the random generating part
      String sarffsa = "A1a2B3b4C5c6D7d8E9eFfGgHhIiKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; 
                while (numofquestionsleft > 0 && m <= numofquestions + 1)
                {
                    int length = a;

                String random = "";

                for (int i = 0; i < length; i++)
                {
                    int alphapet = randomgen.Next(42);
                    random = random + sarffsa.ElementAt(alphapet);
                    
                }
                Console.WriteLine("How many time the symbol has been repeted in the follwing characters: {0}", random);

                 answer = Console.ReadLine();

                // adding the question and the user answer and the right answer to an array
                questionlist[m] = random;
                useranswer = Convert.ToString(Console.ReadLine());
                useranswerlist[m] = answer;
                rightanswerslist[m] = answer;

                Console.WriteLine("--------------------------------------------------------");
                // IF USER TYPE QUIT THAT WILL SKIP THE QUESTION AND ADD IT TO WRONG ANSWERS LIST
                if (answer== quit)
                {
                    numofquestionsleft--;
                    ;
                    continue;

                }
              


                numofquestionsleft--;
                //LOOP
                m++;
            }

            while (1 < 2)
            {
                Console.WriteLine(@"TO GET THE NUMBER OF THE RIGHT ANSWERS PRESS 1
TO GET THE NUMBER OF THE WRONG ANSWERS PRESS 2
TO GET THE OPERATION WITH THE MAX NUMBERS OF RIGHT ANSWERS PRESS 3
TO GET THE OPERATION WITH THE MAX NUMBERS OF FALSE ANSWERS PRESS 4
TO VIEW ALL THE QUESTIONS AND YOUR ANSWERS AND CORRECT ANSWERS TYPE 5
TO EXIT TYPE EXIT
");
                sb = Console.ReadLine();

                b = Int32.Parse(sb);
                switch (b)
                {
                    // the number of right answers 
                    case 1:
                        Console.WriteLine("-----------------------------------------------");
                        Console.WriteLine("You have = " + numofcorrect + " Right answers");
                        Console.WriteLine("-----------------------------------------------");
                        break;
                    case 2:
                        // the number of wrong answers
                        Console.WriteLine("-----------------------------------------------");
                        Console.WriteLine("You have = " + numoffalse + " Wrong answers");
                        Console.WriteLine("-----------------------------------------------");
                        break;
                    // Making the rightoperationlist into a group in order and count to show the most frequent array used
                    case 3:
                        var result = (from operation in rightoperationlist
                                      group operation by operation into og
                                      orderby og.Count() descending
                                      select og.Key).FirstOrDefault();


                        Console.WriteLine("RIGHT ANSWER OPERATION = " + result);

                        break;
                    case 4:
                        // Making the Wrongoperationlist into a group in order and count to show the most frequent array used
                        var result02 = (from operation in wrongoperationlist
                                        group operation by operation into og
                                        orderby og.Count() descending
                                        select og.Key).FirstOrDefault();
                        Console.WriteLine("WRONG ANSWER OPERATIONS = " + result02);

                        break;
                    //showing the questions and user answer and the right answer arrays
                    case 5:
                        Console.WriteLine("QUESTIONS               ANSWERS                RIGHTANSWERS");
                        Console.WriteLine("------------------------------------------------------------------------");
                        for (int z = 0; z < numofquestions; z++)
                        {
                            Console.WriteLine(questionlist[z] + "                 " + useranswerlist[z] + "                   " + rightanswerslist[z]);
                        }
                        Console.WriteLine("------------------------------------------------------------------------");
                        break;

                    default:
                        break;

                }

            }
            Console.ReadLine();




        }
    }
}

【问题讨论】:

  • 如果您想知道为什么在字符串中从未看到 Q 或 Z,请查看您的 randomgen.Next 调用。这是作业题吗?
  • 您的问题到底是什么?我读到这个“我如何在每个问题中选择一个随机字符并询问用户这个字符重复了多少次”。您是否难以选择随机角色?这个字符是否需要包含在随机字符串中,或​​者您是否接受可能为零的答案(如果您的字符串不够长,很可能为零)。那么你问题的第二部分是什么?您需要确定实际答案吗?

标签: c# random char


【解决方案1】:

在生成的随机字符串中选择一个随机字符(作为字符串):

var Symbol = random[randomgen.Next(random.Length)].ToString();

要得到实际答案:

var GameAnswer = random.Count(c => c.ToString() == Symbol);

当然,如果要将随机字符存储为char 而不是string,它会变得更加简单,因为我们可以摆脱两个ToString() 调用。

变成了

var Symbol = random[randomgen.Next(random.Length)]; // Here Symbol type is char

要得到实际答案:

var GameAnswer = random.Count(c => c == Symbol);

【讨论】:

  • 很好奇你为什么用字符串来做这件事..
  • 好评。首先,这是一个临时答案,我什至不确定@safdasfasfasfa 真正想要什么,因此我对他的问题的评论仍未得到回答。
  • 其次,@safdasfasfasfa 在他的代码中只使用字符串,我认为引入 char 变量 (Symbol) 可能会让他感到困惑。我倾向于使用 var 进行变量声明和初始化,因为在 Visual Studio 中,该类型始终可用。它使问题不那么明显。所以我可能也不应该在这种情况下使用 var 让事情更清楚,这可能让我更有勇气使用 char,并避免两个 ToString() 调用
  • @CaiusJard:编辑我的答案以反映您的评论。
猜你喜欢
  • 2017-04-28
  • 2012-12-06
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 2010-12-25
相关资源
最近更新 更多