β Need Help, Basic "lottery game"
Hello! im studying C# and got this project to make a "lottery game" that i have to complete until tomorrow and i cant get Matching number part to work.
i need to Compare matching numbers from user and random and Print out users Numbers, Random Numbers and the matching numbers saying "Bingo Numbers: " and how many numbers are matching.
Can anyone tell me how to make it work or guide how to add the matching numbers of users Array input and random numbers List?
Here's the part if its any help (To much text with the whole code)
//New list for Matching nr
List<int> matchNum = new List<int>();
Console.WriteLine();
Console.Write("Bingo Numbers: ");
//Loop to compare and add matching nr to matchNum and print each matching number as "Bingo"
foreach (int num in rndList)
{
if (rndList.Contains(num))
{
matchNum.Add(iNum);
}
if (matchNum.Count > 0)
{
Console.Write(matchNum.Count + ", ");
}
52 Replies
You can format your code here with $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/Where is the user's list of numbers?
Because at the moment it looks like you're checking if each number in
rndList
is in rndList
Not sure what iNum
is either
You sure you haven't missed some relevant code? If it's too long to go here then you can $pasteIf your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/$codeConsole.WriteLine("Make your Menu Choise: ");
Console.WriteLine("| 1. -> Start Game.);
Console.WriteLine("0. -> Exit.);
//User Menu input:
Console.WriteLine("Menu Choise: ");
int usrMenu = Convert.ToInt32(Console.ReadLine());
if (usrMenu == 1)
{
//Clear Window
Console.Clear();
//Tell user to write 10 Numbers between 1-25
Console.WriteLine("Write 10 Numbers Between 1-25!");
//User Array
int[] usrTen = new int[10];
int i = 0;
//Loop for users input
for (i = 0; i < 10; i++)
{
Console.Write("Write Nr " + (i+1) + ": ");
usrTen[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine();
//Output users Nr:
Console.Write("Your Numbers: ");
for (i = 0; i < 10; i++)
{
Console.Write(usrTen[i] + ", ");
}
Console.WriteLine();
Random rnd = new Random();
//Make a List for random nr
List<int> rndList = new List<int>();
//Loop to add 10 nr between 1-40 in listNum
for (int j = 1; j < 26; j++)
rndList.Add(j);
Console.Write("Random Nr: ");
int iNum = rnd.Next(rndList.Count);
for (int j = 1; j < 10; j++)
{
iNum = rnd.Next(rndList.Count);
Console.Write(rndList[iNum] + ", ");
rndList.RemoveAt(iNum);
}
//----> Here Comes my Problem <----
//New list for Matching nr
List<int> matchNum = new List<int>();
Console.WriteLine();
Console.Write("Your Bingo-Points: ");
//Loop to compare and add matching nr to matchNum
foreach (int num in rndList)
{
if (rndList.Contains(num))
{
matchNum.Add(iNum);
}
if (matchNum.Count > 0)
{
Console.Write(matchNum.Count + ", ");
}
}
}
else if (usrMenu == 0)
{
Console.WriteLine("Game Exiting!\nPress Enter To Close!");
Console.ReadKey();
}
Don't type
$code
follow the instructions in the message
For example $codegif
ahh
thnx
thats the code but i dont know how i compare user input array and random number list and add matching numbers to matchNum and print all elements in matchnum separated by ", ", any tips? π΅βπ«
So here:
the comment suggests you're trying to add 10 numbers that are randomly generated numbers between 1 and 40?
yea, i forgot to change that one, its supposed to be generating 10 random numbers between 1-25, not 1-40 xD
Right, but it's also not generating any random numbers at all
You use a for loop below it so you should be able to tell what it's actually doing?
i have a for loop below, in visual studio i get the random numbers printed out
you will get some random numbers printed out because of what you're doing below yes, because of what you're doing after it, but you're not creating your random list correctly, which is going to cause you issues when you're checking the numbers afterwards
but just 9 of them, missing the 10th
ahh
See, I've added a loop to print out your "random" list
Do you get where you've gone wrong with populating the random list?
looks like im trying to print the numbers the generator is generating not numbers from a list.. am i wrong?
Sort of, the root of the problem is you're simply populating your random list with the numbers 1-25
Do you know what you'd need to do to correct that?
So, in this part i gotta change From:
To:
?
or keep the 1 ? ^^
for (int j = 0; j < 10; j++)
this is correct now
rndList.Add(j);
this still won't get you a random number though, it's still just adding j
to your list (so you will end up with 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)can i do like this in that part:
You do want to use the
rnd
you've created yes
do you know how to use the Random
class to generate a number in the range (1-40) that you want?i get a feeling i thought i understood, but apparently i dont π
Can that be right ?
or maybe i need a new variable for that ^^
Yes you can't do it that way, you have already created an instance of the
Random
class in your code though, Random rnd = new Random();
so you can use that here
And you have already used it in your code, e.g.:
int iNum = rnd.Next(rndList.Count);
In this instance it's generating a random number between 0 and the count of items in rndList
, so you'll want to use it slightly differently
I think this is the right ^^
rnd.Next(1, 25)
is the correct way you'd want to use it, yes (aren't your random numbers supposed to be up to 40 though?)From the beginning is was, but i changed it and forgot to change the comment ^^
Either way, whatever number you want to go up to, the second number you provide should be 1 more than that, e.g. if you want numbers from 1-25 then you should do:
rnd.Next(1, 26)
Oh okay π
so you know what to change your loop to for generating your random list now? You should still only need one line inside the loop
that gotta be a foreach loop? or im totaly off grid? xD because i have the RemoveAt too to remove the duplicate numbers
oh, thats coming in the comparing part.. when im making new list for matchNum.. ^^
Right ok I see what you were originally trying to do then
oh, im sorry if i've not been clearly enough π
So you can update your loop to check if the randomly generated number you're trying to add is already in your list:
Does that make sense?
So it's still doing the loop 10 times to get you 10 random numbers, but each iteration of the loop will keep running until it generates a number that is not already in the list
more than before ^^ But the loop is doing more than 10 duplicate numbers now π¬
or doing 10 duplicate numbers i mean
What do you mean?
What does your current code look like?
Ok, so see the code I sent above,
rnd.Next(1, 26)
has to be inside your loop so that it generates a new number each time, otherwise it will generate a random number at the start and never changeLike this:
oh, dont mind the "int rangeRnd =" removing that
Cool that will generate you a list of random numbers now, looks like you're missing a
)
at the end here:
If you want to make sure they're all unique then you can do something like the code I sentoh yea, i missed ")" ^^
okay, im gonna try that, the do-/while loop right?
yeah that's right
So in total your code here:
would be replaced with:
oh, right, so i generate random numbers to the list in that loop instead and at the same time duplicates are going to be replaced? π
yeah that's right, rather than creating a list which may contain duplicates and then going through it again afterwards
Oh yeah right, and after that im going to be able to easier compare with users array and add matching numbers to matchNum List? ^^
in theory!
sweet, okay, im gonna try that loop and see if i can manage to compare and add matching numbers to the new list and output π
Thank you sooo much for your help! π i've been struggling with this part since almost a week back ^^
i really appriciate it π π
No problem, it can be confusing at the start when you're trying to take in a lot of different things
yea, thats for sure xD thanks againπ
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.