I have seemed to hit a dead-end in my coding of a lottery program in c# and I am looking for some pointer. 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. 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
this is what I would want to do
add the users 7 digits to some sort of list (easy)
add 1000(for example) unique winning lots to a list (2, 1, 16, 32, 5, 9, 17 could be a winner for example)
compare the users 7 numbers to the 1000 winning lots and see how many times I got a certain amount of numbers correct
can I get some pointers or ideas of how I should accomplish this? maybe I can use a 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
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.
add 7 random numbers to a list ~ Use Math > Random to add numbers to a list
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.