[英]Lottery program c# saving and comparing winning numbers in lists
I have seemed to hit a dead-end in my coding of a lottery program in c# and I am looking for some pointer.我似乎在用 C# 编写彩票程序时遇到了死胡同,我正在寻找一些指针。 What I am trying to do is let the user enter 7 numbers, which will be their lottery "ticket" then I want to draw one or multiple 7 digits winning numbers and compare it to the user's number and see how many times they had 5 correct answers, 6 correct answers and so on.
我想要做的是让用户输入 7 个数字,这将是他们的彩票“票”,然后我想绘制一个或多个 7 位数的中奖号码并将其与用户的号码进行比较,看看他们有多少次有 5 个正确答案,6 个正确答案等。 This is what I currently am doing
这就是我目前正在做的
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
}
this does work, but if I draw a winning number 100 000 times, some of the lots will repeat so to say, and i don't want that这确实有效,但是如果我抽中了 100 000 次中奖号码,有些抽签会重复这样说,我不希望那样
this is what I would want to do这就是我想要做的
add the users 7 digits to some sort of list (easy)将用户 7 位数字添加到某种列表中(简单)
add 1000(for example) unique winning lots to a list (2, 1, 16, 32, 5, 9, 17 could be a winner for example)将 1000 个(例如)独特的中奖手数添加到列表中(例如,2、1、16、32、5、9、17 可能是中奖者)
compare the users 7 numbers to the 1000 winning lots and see how many times I got a certain amount of numbers correct将用户的 7 个号码与 1000 个中奖号码进行比较,看看我有多少次猜对了一定数量的号码
can I get some pointers or ideas of how I should accomplish this?我可以得到一些关于我应该如何实现这一点的指示或想法吗? maybe I can use a HashSet?
也许我可以使用HashSet? since they only allow unique numbers, but how would I add the lots to the list since I don't want to add them like this
7321114181923
but rather 7 32 11 14 18 19 23
因为它们只允许唯一的数字,但是我如何将批次添加到列表中,因为我不想像这样添加它们
7321114181923
而是7 32 11 14 18 19 23
Create array list or a int list: Of entered numbers, and of random numbers创建数组列表或整数列表:输入的数字和随机数
"add the users 7 numbers to a list ~ get system input 7 times with loop. “将用户 7 个数字添加到列表中 ~循环获取系统输入 7 次。
add 7 random numbers to a list ~ Use Math > Random to add numbers to a list将 7 个随机数添加到列表中 ~使用 Math > Random 将数字添加到列表中
compare and see how many winnings ~ Compare each number of the list to an entered list, array comparison.比较看看有多少奖金~将列表中的每个数字与输入的列表进行比较,数组比较。
track how many times 5 correct, 6 correct, etc ~ this is where a second counter comes in跟踪 5 次正确、6 次正确等的次数 ~这是第二个计数器出现的地方
loop until number of times I want to draw a winning number ~ this can be tracked with another counter "循环直到我想抽中奖号码的次数〜这可以用另一个计数器跟踪“
Sorry, for a text explanation, you gave me text, so I gave you some text, you gave me nothing to work with.对不起,对于文字解释,你给了我文字,所以我给了你一些文字,你没有给我任何工作。
Also, you can use string manipulation functions to add a " " to each printed number.此外,您可以使用字符串操作函数为每个打印的数字添加一个“”。
One simplistic way of doing this would be to store the winning number combination in a list (as a single winning combination), and then you can have a list of lists that holds all the winning combinations.一种简单的方法是将中奖号码组合存储在一个列表中(作为单个中奖组合),然后您可以拥有一个包含所有中奖组合的列表。
Similarly, store the user's number combination in a list, and then you can see how many matches those numbers had with the winning numbers.类似地,将用户的号码组合存储在一个列表中,然后您可以看到这些号码与中奖号码有多少匹配。
For example:例如:
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...");
}
Output输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.