我的代码未检查所有数组的索引(彩票程序)

时间:2019-06-19 07:10:01

标签: c#

我正在制作彩票程序,它是基本程序,目前只能通过控制台查看。

程序执行以下操作:

  1. 用户输入1到46之间的6个数字。

  2. 程序会生成6个具有相同范围的随机数。

  3. 程序比较索引以查看用户设法与程序匹配的数字。

  4. 程序显示用户正确输入的数字。

但是,当前,我的代码中存在一个错误,我不确定如何继续。

例如,我的输入是:1,2,3,4,5,6

程序生成6个数字,而我设法达到2和6。 但是,该程序仅显示数字2。 这意味着我的代码没有比较每个索引,而且我不确定为什么。

用户数组为lucky,程序生成的数组为numbers

Console.WriteLine("The winning numbers are: , " );

int[] winning = new int[6];
int w = 0;
var x = 0;
var j = 0;
Console.WriteLine("The winning numbers are: , " );

int[] winning = new int[6];
int w = 0;
var x = 0;
var j = 0;
while (x< 6)
{    
   if (lucky[j] == numbers[x])
   {
       winning[w] = numbers[x];
       Console.WriteLine(winning[w]);
       w++;
   }
   j++;

   if (j == 5)
   {
       x++;
       j = 0;
   }
}

5 个答案:

答案 0 :(得分:4)

这些天没有必要进行所有循环。 LINQ的Intersect function使其成为一个函数调用:

var Matches = lucky.Intersect(numbers);

将返回Matches中两个列表中的所有匹配数字。

等效的循环可能看起来像这样(写下我的头顶):

List<int> winning = new List<int>();
for(int i=0; i<numbers.Length; i++)
{
  if(numbers.Contains(lucky[i])
    winning.Add(lucky[i]);
}

要在控制台上显示它,请使用一个简单的循环:

for(int i=0; i<winning.Length; i++)
{
    Console.WriteLine(winning[i]);
}

答案 1 :(得分:2)

由于在数组上运行,因此标准过程是使用for循环。 这是解决问题的三种解决方案。
每个都完整,可以在https://dotnetfiddle.net/

上进行测试

Linq:使用Intersects方法查找两个IEnumerables之间的共同项目。

using System;

using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // at this point unique numbers have been generated and inputted
        int[] numbers = { 1, 2, 3, 4, 5, 6 };
        int[] guesses = { 2, 6, 7, 8, 9, 10 };

        List<int> matches = new List<int>(numbers.Intersect(guesses));
        foreach (int n in matches)
        {
            Console.WriteLine("Hit: " + n.ToString());
        }
    }
}

使用单个for循环,并使用Contains方法检查(数组实现IList接口),如果另一个数组包含当前索引处的数字。您也可以使用foreach循环,因为您不必关心索引。

using System;

using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // at this point unique numbers have been generated and inputted
        int[] numbers = { 1, 2, 3, 4, 5, 6 };
        int[] guesses = { 2, 6, 7, 8, 9, 10 };
        List<int> matches = new List<int>();

        for (int i = 0; i < guesses.Length; i++)
        {
            if (numbers.Contains(guesses[i]))
            {
                Console.WriteLine("Hit: " + guesses[i].ToString());
                matches.Add(guesses[i]);
            }
        }
    }
}

您可以使用嵌套的for循环(每个数组一个)来检查一个数组中的每个数字与另一个数组中的每个数字。
同样,您可以使用foreach循环。

using System;

using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // at this point unique numbers have been generated and inputted
        int[] numbers = { 1, 2, 3, 4, 5, 6 };
        int[] guesses = { 2, 6, 7, 8, 9, 10 };
        List<int> matches = new List<int>();

        for (int i = 0; i < guesses.Length; i++)
        {
            for (int j = 0; j < numbers.Length; j++)
            {
                if (guesses[i] == numbers[j])
                {
                    Console.WriteLine("Hit: " + guesses[i].ToString());
                    matches.Add(guesses[i]);
                    break; // optional, we found the number and can leave the loop. Not optional if your lottery allows numbers to happen more than once.
                }
            }
        }
    }
}

关于您的代码为何不起作用的问题:
您在j = 0之后的j == 5时设置j++,这意味着在检查索引4之后将j设置为0。虽然我不想鼓励这种非正统的样式,但您可以修复它比较j == 6。同样,这种方法使您的代码不可读,请使用其他解决方案之一。

using System;

public class Program
{
    public static void Main()
    {
        // at this point unique numbers have been generated and inputted
        int[] numbers = { 1, 2, 3, 4, 5, 6 };
        int[] guesses = { 2, 6, 7, 8, 9, 10 };

        int[] winning = new int[6];
        int w = 0;
        var x = 0;
        var j = 0;
        while (x < 6)
        {
            if (guesses[j] == numbers[x])
            {
                winning[w] = numbers[x];
                Console.WriteLine(winning[w]);
                w++;
            }
            j++;
            if (j == 6)
            {
                x++;
                j = 0;
            }
        }
    }
}

答案 2 :(得分:0)

我认为您正在寻找的是从两个数组中找到共同的项目。

var ar1 = new int[] {1,2,3,4,5,6}; 
var ar2 = new int[] {2,3,4,6,7,8}; 
var common = ar1.Intersect(ar2);

以您的情况

var common = lucky.Intersect(numbers);

使用循环

// Array size is fixed here.
for (int i = 0; i < 6; i++) // OR i < lucky.Length (guessing numbers)
{
    if (numbers.Contains(lucky[i]))
    {
        // NUMBERS DETECTED
    }
}

答案 3 :(得分:0)

问题在于检查最后一个索引(在这种情况下为5)。这是从未发生

//say j = 4

lucky[4] == numbers[x] // false / true,无论x的值是什么

您增加j //j is now 5

if(j==5)

是,然后将其重置为0。 在这种情况下,最后一个索引为5的索引将不会在下一次迭代中被检查

  

解决方案-   if(j == 6)

答案 4 :(得分:0)

我最终建立了两种方法,这些方法基本上可以完成这里每个人告诉我的事情。 由于我还没有学习过LinQ,所以我也听不懂LIST,所以也无法使用它。

我的功能如下:

第一个功能:检查指定数组的索引 代码:

static bool DoesExists(int[] a, int Value)
        {
            for (int i = 0; i < a.Length; i++)
            {
                if (Value == a[i])
                {
                    return true;
                }
            }

            return false;
        }

第二个函数检查2个数组中有多少个重复的元素 代码:

static int CountCorrect(int[] pc, int[] user)
        {
            int count = 0;
            for (int i = 0; i < user.Length; i++)
            {
                if (DoesExists(pc, user[i]))
                {
                    count++;
                }
            }

            return count;
        }

当将这两个一起使用时,它解决了我的问题。 感谢大家抽出宝贵的时间给我好主意。