繁体   English   中英

彩票程序c#在列表中保存和比较中奖号码

[英]Lottery program c# saving and comparing winning numbers in lists

我似乎在用 C# 编写彩票程序时遇到了死胡同,我正在寻找一些指针。 我想要做的是让用户输入 7 个数字,这将是他们的彩票“票”,然后我想绘制一个或多个 7 位数的中奖号码并将其与用户的号码进行比较,看看他们有多少次有 5 个正确答案,6 个正确答案等。 这就是我目前正在做的

while(a counter < the number of times I want to draw a winning number )
{
 add the users 7 numbers to a list

 add 7 random numbers to a list

 compare and see how many winnings

 track how many times 5 correct, 6 correct, etc

 loop until number of times I want to draw a winning number

}

这确实有效,但是如果我抽中了 100 000 次中奖号码,有些抽签会重复这样说,我不希望那样

这就是我想要做的

  1. 将用户 7 位数字添加到某种列表中(简单)

  2. 将 1000 个(例如)独特的中奖手数添加到列表中(例如,2、1、16、32、5、9、17 可能是中奖者)

  3. 将用户的 7 个号码与 1000 个中奖号码进行比较,看看我有多少次猜对了一定数量的号码

我可以得到一些关于我应该如何实现这一点的指示或想法吗? 也许我可以使用HashSet? 因为它们只允许唯一的数字,但是我如何将批次添加到列表中,因为我不想像这样添加它们7321114181923而是7 32 11 14 18 19 23

创建数组列表或整数列表:输入的数字和随机数

“将用户 7 个数字添加到列表中 ~循环获取系统输入 7 次。

将 7 个随机数添加到列表中 ~使用 Math > Random 将数字添加到列表中

比较看看有多少奖金~将列表中的每个数字与输入的列表进行比较,数组比较。

跟踪 5 次正确、6 次正确等的次数 ~这是第二个计数器出现的地方

循环直到我想抽中奖号码的次数〜这可以用另一个计数器跟踪

对不起,对于文字解释,你给了我文字,所以我给了你一些文字,你没有给我任何工作。

此外,您可以使用字符串操作函数为每个打印的数字添加一个“”。

一种简单的方法是将中奖号码组合存储在一个列表中(作为单个中奖组合),然后您可以拥有一个包含所有中奖组合的列表。

类似地,将用户的号码组合存储在一个列表中,然后您可以看到这些号码与中奖号码有多少匹配。

例如:

private static readonly Random Rnd = new Random();

// A helper function that returns a list of unique, random numbers from 1 to 49
private static List<int> GetRandomLotteryTicket()
{
    var possibilites = Enumerable.Range(1, 49).ToList();
    var ticket = new List<int>();

    for (int i = 0; i < 7; i++)
    {
        // Choose a random number from the possibilites
        var randomNumber = possibilites[Rnd.Next(possibilites.Count)];
        ticket.Add(randomNumber);
        // Then remove it so we don't select it a second time
        possibilites.Remove(randomNumber);
    }

    return ticket;
}

static void Main()
{
    // 5 random lottery tickets added as sample data
    var winningTickets = new List<List<int>>
    {
        GetRandomLotteryTicket(),
        GetRandomLotteryTicket(),
        GetRandomLotteryTicket(),
        GetRandomLotteryTicket(),
        GetRandomLotteryTicket()
    };

    // Normally you'd get this from the user, this is just sample data
    var userTicket = GetRandomLotteryTicket();

    Console.WriteLine($"Your numbers are: {string.Join("-", userTicket)}\n");

    foreach (var winningTicket in winningTickets)
    {
        var matchCount = winningTicket.Intersect(userTicket).Count();
        Console.WriteLine($"You matched {matchCount} numbers " + 
            $"with this ticket: {string.Join("-", winningTicket)}");
    }

    GetKeyFromUser("\nPress any key to exit...");
}

输出

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM