我如何获得我自己的号码来与宾果游戏的随机数字进行核对
本文关键字:随机 游戏 数字 我自己 何获得 自己的 号码 | 更新日期: 2023-09-27 17:59:21
我的宾果游戏应该很简单——你从1到25写10个数字,它应该拿出7个随机数字与我自己的10个数字进行比较,最后我希望它显示结果。
我搞砸了numbers == 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();
}
}
}
这是一个草率的答案,但它可能会帮助您解决问题。
首先,无论try/catch块是否失败,for循环都将运行11次。
int[] bingorow = new int[11];
for (int i = 0; i < bingorow.Length; i++)
第二,当您从catch"继续"时,基本上打印一条消息,程序继续。我建议抛出一个适当的例外。
第三,返回for循环-程序将接受一个值,然后运行所有代码。所以基本上,如果你运气好的话,你会猜出7分之一。然后循环将再次运行第二次,您将获得另一个数字。。。等等。这就是为什么我把catch之后的所有代码都放在循环之外。这样,它将迭代多次(在本例中为7),并从每次迭代的输入中获得一个数字。在存储完所有7个数字后,它将继续执行代码的其余部分。
第四,不确定你是否希望数字在1-25之间。
number[r] = randmNr.Next(1, 25);
这将返回1-24(包括1-24)范围内的数字,因为Next方法的上限是排他性的,正如Mark在这里所说:https://stackoverflow.com/a/5063276/4453195
以下是解决您问题的简单方法:
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();
}
}
不能仅通过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 < arr1.length; i++) //or whichever array is shorter
if(arr1[i] == arr2[i])
//true
//else false