比较C#Lottery Ticket

时间:2015-11-10 14:35:05

标签: c# asp.net-mvc model-view-controller

我正在尝试在C#中构建一个乐透票验证器。编程将允许用户生成一个包含多个机会的票证。每个机会将由6个数字组成。 5个常规数字和1个电源编号。然后,用户输入所抽取的数字,这些数字与故障单上的每个机会进行比较。

对此进行建模并进行比较的最佳方法是什么?

作为一个测试,我已经能够使用两个模型的字符串[]数组表示并迭代它们。但正如你可以看到下面的代码,它只告诉我所有匹配的数量..我需要一种方法来迭代自定义对象并计算每次机会中的匹配数量。此外,如果功率数匹配,我需要一种方法使其成为奖励。最后,这将是一个Web应用程序,所以在我认为,门票将显示在一张桌子上。如何通知哪个号码应突出显示(即匹配)?只是逻辑,我可以处理剩下的......

我从未使用过反射,所以也许这就是答案,但我看到的唯一例子是比较相同类型的对象和不在集合中的对象。

请帮忙..谢谢..

我的测试代码

int matches = 0;

        List<string[]> numbas = new List<string[]>();

        string[] numbers = new string[] { "55", "10", "12", "42" };
        string[] numbers1 = new string[] { "62", "45", "84", "23" };
        string[] numbers2 = new string[] { "2", "23", "45", "43" };
        string[] numbers3 = new string[] { "4", "45", "4", "43" };
        string[] numbers4 = new string[] { "86", "33", "67", "51" };


        numbas.Add(numbers);
        numbas.Add(numbers1);
        numbas.Add(numbers2);
        numbas.Add(numbers3);
        numbas.Add(numbers4);

        string[] drawn = new string[] { "51", "10", "33" };



        for (int rows = 0; rows <= numbas.Count-1; rows++)
        {
            for (int drawNum = 0; drawNum <= drawn.Length-1; drawNum++)
            {
                for (int tickNo = 0; tickNo <= numbas[rows].Length-1; tickNo++)
                {
                    Console.WriteLine("Drawn Number {0} being comparted to number {1}", drawn[drawNum], numbas[rows].ElementAt(tickNo));
                    if (drawn[drawNum] == numbas[rows].ElementAt(tickNo))
                    {
                        matches++;
                    }
                }


            }

        }

        Console.WriteLine("There were " + matches + " matches.");
        Console.ReadLine();

这是我到目前为止实际的程序。

我的模特

    public class Ticket
{
    public int TicketID { get; set; }
    public string TicketName { get; set; }
    public DateTime DrawDate { get; set; }

    public virtual ICollection<Chance> Chances { get; set; }
    public virtual DrawnNumber DrawnNumber { get; set; }

}


public class Chance
{
    public int RowID { get; set; }
    public int TicketID { get; set; }

    public string Number1 { get; set; }
    public string Number2 { get; set; }
    public string Number3 { get; set; }
    public string Number4 { get; set; }
    public string Number5 { get; set; }

    public string PowerNumber { get; set; }


}

public class DrawnNumber
{
    public int DrawnNumberID { get; set; }
    public int TicketID { get; set; }

    public string DrawnNumber1 { get; set; }
    public string DrawnNumber2 { get; set; }
    public string DrawnNumber3 { get; set; }
    public string DrawnNumber4 { get; set; }
    public string DrawnNumber5 { get; set; }

    public string DrawnPowerNumber { get; set; }

}

1 个答案:

答案 0 :(得分:1)

在仔细考虑了这个场景之后,我发现最好只为每个乐透号码创建更复杂的对象。这样它就会给我每个数字的功能(即如果选择了数字则设置为true)

而不是:

public int Number1

执行:

Class Number 

{
   int value;
   bool isTrue;

}

class Number1 : Number 

{

}

同样使我的Chance类是包含乐透号码的字段,从IEnumberable派生,以实现GetEnumerator方法迭代所有字段..(检查中奖号码)

像:

 public class Chance : IEnumerable
        {
            public string chanceNumber;
            public FirstNumber FirstNumber;
            public SecondNumber SecondNumber;
            public ThirdNumber ThirdNumber;
            public FourthNumber FourthNumber;
            public FifthNumber FifthNumber;
            public PowerNumber PowerNumber;
            public Number[] numbers;

            public Chance()
            {
            }


            public IEnumerator GetEnumerator()
            {
                numbers = new Number[] { FirstNumber, SecondNumber, ThirdNumber, FourthNumber, FifthNumber, PowerNumber};      
                return new NumberEnumerator(numbers);
            }
}

像魅力一样! 感谢