简体   繁体   English

如何取最频繁的数加入数组 c#

[英]How to take the most frequent number added to the array c#

my code is a math game with rand numbers and operations at the end i ask the user if they want to show the operation with the most wrong/right answers i made each time the user answers right/wrong it will add the operation to a WrongOperationArray/RightOperationArray but how do i show the most frequent operation in each array我的代码是一个数学游戏,最后有随机数和运算/RightOperationArray 但我如何显示每个数组中最频繁的操作

     string[] operators = new string[4]{ "+", "-", "*", "/" };
          
     
            }
            // LOOP THAT WILL SHOW THE QUESTIONS AND ADD THE RIGHT ANSWERS AND WRONG TO A LIST .
        
            string[] rightoperationlist = new string[numofquestions];
            string[] wrongoperationlist = new string[numofquestions];
            while (numofquestionsleft > 0 && m <=numofquestions+1)
            {
              
                  string op = operators[randomgen.Next(operators.Length)];
                 
                      switch (op)
                      {
                          case "+":
                              answer = num01 + num02;

                              break;

                          case "-":
                              answer = num01 - num02;

                              break;
                          case "*":
                              answer = num01 * num02;

                              break;
                          case "/":
                              answer = Math.Round((double)num01 / (double)num02, 2);

                              break;
                      }

                      sss = Convert.ToString(answer);
                      //THE QUESTION
                      question = string.Format("{0} {1} {2}", num01, op, num02);


                      Console.WriteLine("--------------------------------------------------------");
                      Console.WriteLine("What is = " + question + " Or type QUIT to ignore ");

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

                      }
                      if (useranswer == sss)
                      //IF USER ANSWER IS CORRECT THE NUMBER OF CORRECT ANSWERS WILL +1 && add the operation to the array
                      {
                          numofcorrect++;
                          rightoperationlist[m] = op;

                      }
                      else
                      //IF USER ANSWER IS WRONG THE NUMBER OF WRONG ANSWERS WILL +1 && add the operation to the array
                      {
                          numoffalse++;
                          wrongoperationlist[m] = op;

                      }


                      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)
                {

                    case 1:
                        Console.WriteLine("-----------------------------------------------");
                        Console.WriteLine("You have = " + numofcorrect + " Right answers");
                        Console.WriteLine("-----------------------------------------------");
                        break;
                    case 2:
                        Console.WriteLine("-----------------------------------------------");
                        Console.WriteLine("You have = " + numoffalse + " Wrong answers");
                        Console.WriteLine("-----------------------------------------------");
                        break;
                    case 3:
                  
                        for (int l=0;  l< numofcorrect;l++ )
                        {
                            Console.WriteLine("RIGHT ANSWER OPERATION = " + rightoperationlist[l]);
                        }            
                            break;
                    case 4:              
                            for (int k = 0; k < numoffalse; k++)
                            {
                                Console.WriteLine("WRONG ANSWER OPERATIONS = "+ wrongoperationlist[k]);
                            }  
                        break;
                    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();

You can use linq, there is example:您可以使用 linq,例如:

var result = (from operation in rightoperationlist
         group operation by operation into og
         orderby og.Count() descending
         select og.Key).FirstOrDefault();

Based on my test, you can select the operation and the count of the array first.根据我的测试,可以先select对数组的运算和计数。

Then, you can select the max count of the list.然后,您可以 select 列表的最大计数。

Finally, you can get the correct result.最后,您可以获得正确的结果。

 static void Main(string[] args)
        {
            string[] operators = new string[4] { "+", "-", "*", "/" };
            int numofquestions = 6;
            int numofquestionsleft = 2;
            Random random = new Random();
            double answer = 0;
            Console.WriteLine("Please enter two numbers");
            double num01 = Convert.ToInt32(Console.ReadLine());
            double num02 = Convert.ToInt32(Console.ReadLine());
            int m = 0 ;
            string[] rightoperationlist = new string[numofquestions];
            string[] wrongoperationlist = new string[numofquestions];
            int numofcorrect = 0;
            int numoffalse = 0;
            while (numofquestionsleft > 0 && m <= numofquestions-1 )
            {

                string op = operators[random.Next(operators.Length)];

                switch (op)
                {
                    case "+":
                        answer = num01 + num02;

                        break;

                    case "-":
                        answer = num01 - num02;

                        break;
                    case "*":
                        answer = num01 * num02;

                        break;
                    case "/":
                        answer = Math.Round((double)num01 / (double)num02, 2);

                        break;
                }

                string sss = Convert.ToString(answer);
                //THE QUESTION
                string question = string.Format("{0} {1} {2}", num01, op, num02);


                Console.WriteLine("--------------------------------------------------------");
                Console.WriteLine("What is = " + question + " Or type QUIT to ignore ");

                Console.WriteLine("--------------------------------------------------------");
                string useranswer = Console.ReadLine();
                // IF USER TYPE QUIT THAT WILL SKIP THE QUESTION AND ADD IT TO WRONG ANSWERS LIST

                if (useranswer == sss)
                //IF USER ANSWER IS CORRECT THE NUMBER OF CORRECT ANSWERS WILL +1 && add the operation to the array
                {
                    Console.WriteLine("correct answer!");
                    numofcorrect++;
                    rightoperationlist[m] = op;

                }
                else
                //IF USER ANSWER IS WRONG THE NUMBER OF WRONG ANSWERS WILL +1 && add the operation to the array
                {
                    Console.WriteLine("Incorrect Answer");
                    numoffalse++;
                    wrongoperationlist[m] = op;

                }

                //numofquestionsleft--;
                //LOOP
                m++;
            }
            var correctresult= rightoperationlist.ToList().GroupBy(x => x)
              .Where(g => g.Count() >=1)
              .Select(y => new { Element = y.Key, Counter = y.Count() })
              .ToList();
            Console.WriteLine("The most frequent operation and the count in the correct array");
            int max1 = correctresult.Max(r => r.Counter);
            foreach (var item in correctresult)
            {
                if(item.Element!=null&&item.Counter==max1)
                {
                    Console.WriteLine("The most operation is {0} and the count is {1}", item.Element, item.Counter);
                } 
            }
            var wrongresult = wrongoperationlist.ToList().GroupBy(x => x)
              .Where(g => g.Count()>=1)
              .Select(y => new { Element = y.Key, Counter = y.Count() })
              .ToList();
            Console.WriteLine("The most frequent operation and the count in the wrong array");
            int max2 = wrongresult.Max(r => r.Counter);
            foreach (var item in wrongresult)
            {
                if(item.Element!=null&&item.Counter==max2)
                {
                    Console.WriteLine("The most operation is {0} and the count is {1}", item.Element, item.Counter);
                }

            }
            Console.WriteLine("success");
            Console.ReadLine();

        }

Tested result:测试结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM