简体   繁体   English

如何让我自己的号码与宾果游戏的随机数核对

[英]How do I get my own number to check with the random numbers for bingo

My bingo game should be very simple you write 10 numbers from 1 - 25 and it should take out 7 random numbers to compare with my own 10 numbers, and in the end I want it to show the result.我的宾果游戏应该很简单,你写 1 - 25 的 10 个数字,它应该取出 7 个随机数与我自己的 10 个数字进行比较,最后我希望它显示结果。

I got messed up with numbers == bingorow .我搞砸了numbers == bingorow How to make it check how many right I had?如何让它检查我有多少权利? Here's my code:这是我的代码:

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();
        }
    }
}

This is a sloppy answer, but it might help you with your problem.这是一个草率的答案,但它可能会帮助您解决问题。

First, your for loop will run 11 times regardless of whether the try/catch block fails.首先,无论 try/catch 块是否失败,您的 for 循环都将运行11次。

int[] bingorow = new int[11];
for (int i = 0; i < bingorow.Length; i++)

Second, when you "continue" from the catch, you basically print a message and the program continues.其次,当您从捕获中“继续”时,您基本上会打印一条消息并且程序继续。 I would advise to throw an appropriate exception.我建议抛出一个适当的异常。

Third, back to the for loop - the program will accept a value and then run all the code.第三,回到 for 循环——程序将接受一个值,然后运行所有代码。 So basically you would guess 1 out of 7 if you are lucky.所以基本上,如果你幸运的话,你会猜出 7 个中的 1 个。 Then the loop will run again for a second time and you would get another number... etc. This is why I've taken all the code after the catch outside the loop.然后循环将再次运行第二次,你会得到另一个数字......等等。这就是为什么我在循环之外捕获了所有代码。 That way it will iterate a number of times (in this case 7) and get a number from the input on each iteration.这样它将迭代多次(在本例中为 7)并在每次迭代时从输入中获取一个数字。 After all 7 numbers have been stored, it will carry on with the rest of the code.在存储了所有 7 个数字后,它将继续处理其余的代码。

Fourth, not sure if you want the numbers to be in the range 1-25 inclusive.第四,不确定您是否希望数字在 1-25 之间。

number[r] = randmNr.Next(1, 25);

This will return numbers in the range 1-24 inclusive as the upper bound of the Next method is exclusive as Mark has stated here: https://stackoverflow.com/a/5063276/4453195这将返回 1-24 范围内的数字,因为 Next 方法的上限是独占的,正如 Mark 在此处所述: https : //stackoverflow.com/a/5063276/4453195

Here is a simple solution to your problem:这是您的问题的简单解决方案:

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();
   }
}

You can't just compare two arrays via arr1 == arr2 .您不能仅通过arr1 == arr2比较两个数组。 In c#, arrays are references to different objects.在 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 provides many different methods for comparing arrays and list structures. .Net 提供了许多不同的方法来比较数组和列表结构。 For .NET 4.0 and higher, Structural Comparisons can be used.对于 .NET 4.0 及更高版本,可以使用结构比较 Else a much simpler version would be to use:否则一个更简单的版本是使用:

int arr1[] = new int[] {1, 2, 3};
int arr2[] = new int[] {1, 2, 3};
Console.WriteLine(arr1.SequenceEqual(arr2)); //Done using Linq.

Change number == bingorow to number.SequenceEqual(bingorow) to check if the arrays themselves are equal.number == bingorow更改为number.SequenceEqual(bingorow)以检查数组本身是否相等。 If you're looking for the individual elements that are correct, then you'll need to do a nested loop or some form of for loop that checks each individual value in the array.如果您要查找正确的单个元素,则需要执行嵌套循环或某种形式的for循环来检查数组中的每个单独值。

Ex:前任:

for( int i = 0; i < arr1.length; i++) //or whichever array is shorter
    if(arr1[i] == arr2[i])
       //true
    //else false

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

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