我正在尝试制作一个程序,该程序生成从1到49的6个唯一数字,奖金数字和7位额外数字,我想检查这6位数字是否与当前乐透号码匹配或奖金号码匹配。我还想检查7个额外数字中的任何一个是否匹配。我不确定如何将每个数组中的数字相互比较。
任何帮助将不胜感激
static void PLayLotto649()
{
int[] currentNumbers = ChangeLotto49Numbers();
int[] extraNumbers = ChangeLottoExtraNumbers();
int[] userLottoNumbers = StartingNumbers();
int[] userExtraNumbers = AutoGenrateExtraNumbers();
Console.WriteLine($"\nThe current Lotto 6/49 number are: {string.Join(",",currentNumbers)}" );
Console.WriteLine($"\nThe current Extra number is:{string.Join(",", extraNumbers)}");
Console.WriteLine($"\nYour 6/49 number is: { string.Join(",", userLottoNumbers)}");
Console.WriteLine($"\nYour lotto Extra number is: { string.Join(",", userExtraNumbes)}");
}
static int[] StartingNumbers()
{
int temp;
int[] lotto = new int[7];
Random rand = new Random();
for (int i = 0; i < 7; i++)
{
temp = rand.Next(1, 50);
lotto[i] = temp;
}
return lotto;
}
static int[] ChangeLottoExtraNumbers()
{
int temp;
int[] extra = new int[7];
Random rand = new Random();
for (int i = 0; i < 7; i++)
{
temp = rand.Next(0, 7);
extra[i] = temp;
}
Console.Write($"the new extra number is: ");
for (int i = 0; i < 7; i++)
{
Console.Write(extra[i] + " ");
}
return extra;
}
static int[] AutoGenrateExtraNumbers()
{
int temp;
int[] autoExtra = new int[7];
Random rand = new Random();
for (int i = 0; i < 7; i++)
{
temp = rand.Next(0, 7);
autoExtra[i] = temp;
}
Console.Write($"the new extra number is: ");
for (int i = 0; i < 7; i++)
{
Console.Write(autoExtra[i] + " ");
}
return autoExtra;
}
static int[] ChangeLotto49Numbers()
{
int temp;
int[] lotto49 = new int[6];
Random rand = new Random();
for (int i = 0; i < 6; i++)
{
temp = rand.Next(1, 49);
lotto49[i] = temp;
}
Console.Write($"the new lotto Max winning numbers are: ");
for (int i = 0; i < 6; i++)
{
Console.Write(lotto49[i] + " ");
}
return lotto49;
}
答案 0 :(得分:1)
尝试这样的事情:
static bool IsEqualTo(this int[] source, int[] target) {
if (source == null && target != null ||
source != null && target == null ||
source?.Length ?? 0 != target?.Length ?? 0) {
return false;
}
if (source == null && target == null) {
return true;
}
for (var i = 0; i < source.Length; i++) {
if (source[i] != target[i]) {
return false;
}
}
return true;
}
然后使用:
int[] currentNumbers = ChangeLotto49Numbers();
int[] extraNumbers = ChangeLottoExtraNumbers();
if (currentNumbers.IsEqualTo(extraNumbers)) { // do something
}