C# 我怎样才能得到我自己的数字来和宾果游戏的随机数字进行核对

C# 我怎样才能得到我自己的数字来和宾果游戏的随机数字进行核对,c#,C#,我的宾果游戏应该很简单,你从1-25写10个数字,它应该拿出7个随机数与我自己的10个数字进行比较,最后我希望它能显示结果 我被数字==bingorow搞砸了。如何让它检查我有多少权利?这是我的密码: static void Main(string[] args) { int right = 0; bool foundNr = false; int[] bingorow = new int[11]; for (int i = 0; i < bingorow.

我的宾果游戏应该很简单,你从1-25写10个数字,它应该拿出7个随机数与我自己的10个数字进行比较,最后我希望它能显示结果

我被
数字==bingorow
搞砸了。如何让它检查我有多少权利?这是我的密码:

static void Main(string[] args)
{
    int right = 0;
    bool foundNr = false;
    int[] bingorow = new int[11];
    for (int i = 0; i < bingorow.Length; i++)
    {
        try
        {
            Console.WriteLine("Welcome to C Sharp Bingo!");
            Console.Write("Write down 10 numbers here: ");
            int bingonr = int.Parse(Console.ReadLine());
            bingorow[i] = bingonr;
        }
        catch
        {
            Console.WriteLine("Please write 10 numbers!");
            continue;
        }

        int[] number = new int[7];
        Random randmNr = new Random();
        for (int r = 0; r < number.Length; r++)
        {
            number[r] = randmNr.Next(1, 25);
        }

        if (number == bingorow)
        {
            foundNr = true;
        }

        if (foundNr == true)
        {
            right++;
        }
        {
            Console.WriteLine("  Your score: {0} of 7", right);
            Console.ReadLine();
        }
    }
}
static void Main(字符串[]args)
{
int right=0;
bool foundNr=假;
int[]bingorow=新int[11];
对于(int i=0;i
您不能仅通过
arr1==arr2
比较两个数组。在c#中,数组是对不同对象的引用

int arr1[] = new int[] {1, 2, 3};
int arr2[] = new int[] {1, 2, 3};
if (arr1 == arr2) // Same as arr1.equals(arr2)
    Console.WriteLine("Same");
else
    Console.WriteLine("Not same"); //Will print not same.
.Net提供了许多不同的方法来比较数组和列表结构。对于.NET 4.0及更高版本,可以使用。否则,更简单的版本是使用:

int arr1[] = new int[] {1, 2, 3};
int arr2[] = new int[] {1, 2, 3};
Console.WriteLine(arr1.SequenceEqual(arr2)); //Done using Linq.
number==bingorow
更改为
number.SequenceEqual(bingorow)
,以检查数组本身是否相等。如果要查找正确的单个元素,则需要执行嵌套循环或某种形式的
for
循环来检查数组中的每个值

例:

for(int i=0;i
这是一个草率的答案,但它可能会帮助您解决问题

首先,无论try/catch块是否失败,for循环都将运行11次

int[] bingorow = new int[11];
for (int i = 0; i < bingorow.Length; i++)
这将返回范围为1-24(包括1-24)的数字,因为下一个方法的上限是独占的,如Mark在此处所述:

以下是您的问题的简单解决方案:

static void Main(string[] args)
{
    int right = 0;
    int[] bingorow = new int[7]; // Correct me if I'm wrong but I think a 7 number bingo should accept 7 numbers as input
    string[] positions = { "first", "second", "third", "fourth", "fifth", "sixth", "seventh" }; // This is not necessary, but makes the flow slightly clearer.

    Console.WriteLine("#########################");
    Console.WriteLine("Welcome to C Sharp Bingo!");
    Console.WriteLine("#########################");
    Console.WriteLine("Please provide your 7 numbers in the range from 1 to 25.");

    for (int i = 0; i < bingorow.Length; i++)
    {
        try
        {
            Console.WriteLine("Enter your {0} number:", positions[i]);
            int bingonr = int.Parse(Console.ReadLine());
            bingorow[i] = bingonr;
        }

        catch
        {
            Console.WriteLine("Some error message.");
            // Some Exception should be thrown here don't just use "continue".
            continue;
        }
    }

   int[] numbers = new int[7];
   Random randmNr = new Random();
   for (int r = 0; r < numbers.Length; r++)
   {
       // randmNr.Next(1, 26) will return numbers in the range 1-25 inclusive.
       numbers[r] = randmNr.Next(1, 26);
   }

   // Loop through each number from the input (bingorow) array and check if it is contained in the "winning" (numbers) array
   for (int i = 0; i < bingorow.Length; i++)
   {
       if (numbers.Contains(bingorow[i]))
       {
           right++; // Increment counter on each match.
       }
   }

   {
       Console.WriteLine();
       Console.WriteLine("### Your score: {0} of 7 ###", right);
       Console.Write("Your numbers:");

       // Print the input numbers.
       foreach (int number in bingorow)
       {
           Console.Write(" {0}", number); // Will not be sorted.
       }

       Console.WriteLine();

       Console.Write("Winning numbers:");

       // Print the winning numbers.
       foreach (int number in numbers)
       {
           Console.Write(" {0}", number); // Will not be sorted.
       }

       Console.WriteLine();

       Console.WriteLine("Press Enter to exit.");
       Console.ReadLine();
   }
}
static void Main(字符串[]args)
{
int right=0;
int[]bingorow=new int[7];//如果我错了,请纠正我,但我认为7号宾果应该接受7号作为输入
string[]positions={“first”、“second”、“third”、“fourth”、“fifth”、“sixth”、“seventh”};//这不是必需的,但会使流程稍微清晰一些。
Console.WriteLine(“############################”;
控制台.WriteLine(“欢迎来到C夏普宾果游戏!”);
Console.WriteLine(“############################”;
Console.WriteLine(“请提供1到25之间的7个数字”);
对于(int i=0;i
一个新的SO用户,显然试图发布一个包含详细信息和代码的问题,但却在没有解释性评论的情况下被否决了?这将如何帮助他/她下次发布一个更好的问题?我没有投反对票,但我注意到这里有很多无关的代码。这个问题实际上归结为:如何找到两个数组的组合?@sstan帮助中心充满了关于如何发布高质量问题、提问时应提供哪些信息等信息。他们甚至在被允许提问之前链接到这样一个页面,并且必须确认他们已经阅读了该页面。因此,他们已经具备了提出一个好问题所需的一切手段,他们选择不提出,但并不反对。我只是觉得OP并没有“选择”发布一个糟糕的问题,它只是这样出来的。这肯定是一个比大多数第一篇文章更好的问题,它绝对不值得5票否决。(并不是说这是一个很好的问题。)@xxbbcc他们选择不阅读关于如何提出一个好问题的说明,或者他们选择不遵循这些说明,即使他们没有发布他们知道不好的问题。但考虑到他们所说的“请不要对我的代码太苛刻”和“这里乱七八糟”似乎表明
number[r] = randmNr.Next(1, 25);
static void Main(string[] args)
{
    int right = 0;
    int[] bingorow = new int[7]; // Correct me if I'm wrong but I think a 7 number bingo should accept 7 numbers as input
    string[] positions = { "first", "second", "third", "fourth", "fifth", "sixth", "seventh" }; // This is not necessary, but makes the flow slightly clearer.

    Console.WriteLine("#########################");
    Console.WriteLine("Welcome to C Sharp Bingo!");
    Console.WriteLine("#########################");
    Console.WriteLine("Please provide your 7 numbers in the range from 1 to 25.");

    for (int i = 0; i < bingorow.Length; i++)
    {
        try
        {
            Console.WriteLine("Enter your {0} number:", positions[i]);
            int bingonr = int.Parse(Console.ReadLine());
            bingorow[i] = bingonr;
        }

        catch
        {
            Console.WriteLine("Some error message.");
            // Some Exception should be thrown here don't just use "continue".
            continue;
        }
    }

   int[] numbers = new int[7];
   Random randmNr = new Random();
   for (int r = 0; r < numbers.Length; r++)
   {
       // randmNr.Next(1, 26) will return numbers in the range 1-25 inclusive.
       numbers[r] = randmNr.Next(1, 26);
   }

   // Loop through each number from the input (bingorow) array and check if it is contained in the "winning" (numbers) array
   for (int i = 0; i < bingorow.Length; i++)
   {
       if (numbers.Contains(bingorow[i]))
       {
           right++; // Increment counter on each match.
       }
   }

   {
       Console.WriteLine();
       Console.WriteLine("### Your score: {0} of 7 ###", right);
       Console.Write("Your numbers:");

       // Print the input numbers.
       foreach (int number in bingorow)
       {
           Console.Write(" {0}", number); // Will not be sorted.
       }

       Console.WriteLine();

       Console.Write("Winning numbers:");

       // Print the winning numbers.
       foreach (int number in numbers)
       {
           Console.Write(" {0}", number); // Will not be sorted.
       }

       Console.WriteLine();

       Console.WriteLine("Press Enter to exit.");
       Console.ReadLine();
   }
}