两个C#数组之间的差异(随机输入+用户输入)

时间:2019-06-01 09:08:26

标签: c# arrays compare difference

我创建了一个生成10个随机数的数组,并且希望将其与10个数字的用户输入进行比较。如果6个数字等于随机生成的数字(从1到25),那么它应该显示6。

此外,顺序也不重要。如果用户输入数字8作为他或她的第一个选择,则如果它生成数字8作为其最后选择,则应该能够将其与随机生成的数字进行比较。最后应显示最小值1的结果。

        Random r = new Random();
    int MyRandomNr = r.Next(1, 26);
    // Random Generator

    var ArrayRandom = new int[] { MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr };

    Console.Write("HELLO AND WELCOME!"
    + System.Environment.NewLine + "Type in 10 numbers."
    + System.Environment.NewLine + "A number between from 1 to 25, one number at the time " + System.Environment.NewLine);


    string[] ArrayUser = new string[10];
    for (int i = 0; i < 10; i++)
    {
        Console.Write((i + 1) + ". Next number is: ");
        ArrayUser [i] = Console.ReadLine();
    }

我已经检查了Stackoverflow,下面的代码并没有真正按照我希望它们相互比较的方式比较两个数组。以下代码更像是考试,但我正在寻找与此类似的东西...

            int[] answer = { 1, 3, 4};
            int[] exam = { 4, 1, 3};

            int correctAnswers = 0;
            int wrongAnswers = 0;

            for (int index = 0; index < answer.Length; index++)
            {
                if (answer[index] == exam[index])
                {
                    correctAnswers += 1;
                }
                else
                {
                    wrongAnswers += 1;
                }
            }

            Console.Write("The matching numbers are " + correctAnswers +
                "\n" + "The non matching numbers are " + wrongAnswers);

0 个答案:

没有答案