我的宾果游戏应该非常简单,你从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();
}
}
}
答案 0 :(得分:1)
这是一个草率的答案,但它可能会帮助您解决问题。
首先,无论try / catch块是否失败,你的for循环都会运行 11 次。
int[] bingorow = new int[11];
for (int i = 0; i < bingorow.Length; i++)
其次,当您从捕获中“继续”时,您基本上会打印一条消息并继续执行该程序。我建议抛出一个适当的例外。
第三,回到for循环 - 程序将接受一个值,然后运行所有代码。所以基本上如果你幸运,你会猜到7分中的1分。然后循环将再次运行第二次,你会得到另一个数字......等等。这就是为什么我在循环外捕获后的所有代码。这样它将迭代多次(在这种情况下为7)并从每次迭代的输入中获得一个数字。在存储了所有7个数字之后,它将继续执行其余的代码。
第四,不确定您是否希望数字在1-25范围内。
number[r] = randmNr.Next(1, 25);
这将返回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();
}
}
答案 1 :(得分:0)
您不能只通过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及更高版本,可以使用Structural Comparisons。否则将使用更简单的版本:
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