C
C#β€’7mo ago
XLnTz

βœ… 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
SinFluxx
SinFluxxβ€’7mo ago
You can format your code here with $code
MODiX
MODiXβ€’7mo ago
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/
SinFluxx
SinFluxxβ€’7mo ago
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 $paste
MODiX
MODiXβ€’7mo ago
If 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!
XLnTz
XLnTzβ€’7mo ago
$code
MODiX
MODiXβ€’7mo ago
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/
XLnTz
XLnTzβ€’7mo ago
$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(); }
SinFluxx
SinFluxxβ€’7mo ago
Don't type $code follow the instructions in the message For example $codegif
XLnTz
XLnTzβ€’7mo ago
Console.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();
}
Console.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();
}
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? πŸ˜΅β€πŸ’«
SinFluxx
SinFluxxβ€’7mo ago
So here:
//Loop to add 10 nr between 1-40 in listNum
for (int j = 1; j < 26; j++)
rndList.Add(j);
//Loop to add 10 nr between 1-40 in listNum
for (int j = 1; j < 26; j++)
rndList.Add(j);
the comment suggests you're trying to add 10 numbers that are randomly generated numbers between 1 and 40?
XLnTz
XLnTzβ€’7mo ago
yea, i forgot to change that one, its supposed to be generating 10 random numbers between 1-25, not 1-40 xD
SinFluxx
SinFluxxβ€’7mo ago
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?
XLnTz
XLnTzβ€’7mo ago
i have a for loop below, in visual studio i get the random numbers printed out
//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);
}
//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);
}
SinFluxx
SinFluxxβ€’7mo ago
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
XLnTz
XLnTzβ€’7mo ago
but just 9 of them, missing the 10th ahh
SinFluxx
SinFluxxβ€’7mo ago
No description
SinFluxx
SinFluxxβ€’7mo ago
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?
XLnTz
XLnTzβ€’7mo ago
looks like im trying to print the numbers the generator is generating not numbers from a list.. am i wrong?
SinFluxx
SinFluxxβ€’7mo ago
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?
XLnTz
XLnTzβ€’7mo ago
So, in this part i gotta change From:
for (int j = 1; j < 26; j++)
rndList.Add(j);
for (int j = 1; j < 26; j++)
rndList.Add(j);
To:
for (int j = 0; j < 10; j++)
rndList.Add(j);
for (int j = 0; j < 10; j++)
rndList.Add(j);
? or keep the 1 ? ^^
SinFluxx
SinFluxxβ€’7mo ago
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)
XLnTz
XLnTzβ€’7mo ago
can i do like this in that part:
int iNum = rnd.Next(rndList.Count);
for (int j = 0; j < 10; j++)
rndList.Add(iNum);
int iNum = rnd.Next(rndList.Count);
for (int j = 0; j < 10; j++)
rndList.Add(iNum);
SinFluxx
SinFluxxβ€’7mo ago
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?
XLnTz
XLnTzβ€’7mo ago
i get a feeling i thought i understood, but apparently i dont πŸ˜“
Random rnd = new Random(1, 25);
Random rnd = new Random(1, 25);
Can that be right ? or maybe i need a new variable for that ^^
SinFluxx
SinFluxxβ€’7mo ago
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
XLnTz
XLnTzβ€’7mo ago
Random rnd = new Randon();
rangeRnd = rnd.Next(1, 25)
Random rnd = new Randon();
rangeRnd = rnd.Next(1, 25)
I think this is the right ^^
SinFluxx
SinFluxxβ€’7mo ago
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?)
XLnTz
XLnTzβ€’7mo ago
From the beginning is was, but i changed it and forgot to change the comment ^^
SinFluxx
SinFluxxβ€’7mo ago
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)
XLnTz
XLnTzβ€’7mo ago
Oh okay πŸ˜„
SinFluxx
SinFluxxβ€’7mo ago
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
XLnTz
XLnTzβ€’7mo ago
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.. ^^
SinFluxx
SinFluxxβ€’7mo ago
Right ok I see what you were originally trying to do then
XLnTz
XLnTzβ€’7mo ago
oh, im sorry if i've not been clearly enough πŸ™ˆ
SinFluxx
SinFluxxβ€’7mo ago
So you can update your loop to check if the randomly generated number you're trying to add is already in your list:
int number;
for (int j = 0; j < 10; j++)
{
do {
number = rnd.Next(1, 26);
} while (rndList.Contains(number));
rndList.Add(number);
}
int number;
for (int j = 0; j < 10; j++)
{
do {
number = rnd.Next(1, 26);
} while (rndList.Contains(number));
rndList.Add(number);
}
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
XLnTz
XLnTzβ€’7mo ago
more than before ^^ But the loop is doing more than 10 duplicate numbers now 😬 or doing 10 duplicate numbers i mean
SinFluxx
SinFluxxβ€’7mo ago
What do you mean? What does your current code look like?
XLnTz
XLnTzβ€’7mo ago
Random rnd = new Random();

List<int> rndList = new List<int>();

int rangeRnd = rnd.Next(1, 26);

int iNum = rnd.Next(rndList.Count);

//Loop to add 10 nr between 1-25 in listNum
for (int j = 0; j < 10; j++)
rndList.Add(rangeRnd);

Console.Write("Random nr: ");
foreach (int num in rndList)
Console.Write(num + ", ");
Random rnd = new Random();

List<int> rndList = new List<int>();

int rangeRnd = rnd.Next(1, 26);

int iNum = rnd.Next(rndList.Count);

//Loop to add 10 nr between 1-25 in listNum
for (int j = 0; j < 10; j++)
rndList.Add(rangeRnd);

Console.Write("Random nr: ");
foreach (int num in rndList)
Console.Write(num + ", ");
SinFluxx
SinFluxxβ€’7mo ago
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 change
XLnTz
XLnTzβ€’7mo ago
Like this:
Random rnd = new Random();

List<int> rndList = new List<int>();

int rangeRnd =

int iNum = rnd.Next(rndList.Count);

//Loop to add 10 nr between 1-25 in listNum
for (int j = 0; j < 10; j++)
rndList.Add(rnd.Next(1, 26);

Console.Write("Random nr: ");
foreach (int num in rndList)
Console.Write(num + ", ");
Random rnd = new Random();

List<int> rndList = new List<int>();

int rangeRnd =

int iNum = rnd.Next(rndList.Count);

//Loop to add 10 nr between 1-25 in listNum
for (int j = 0; j < 10; j++)
rndList.Add(rnd.Next(1, 26);

Console.Write("Random nr: ");
foreach (int num in rndList)
Console.Write(num + ", ");
oh, dont mind the "int rangeRnd =" removing that
SinFluxx
SinFluxxβ€’7mo ago
Cool that will generate you a list of random numbers now, looks like you're missing a ) at the end here:
rndList.Add(rnd.Next(1, 26);
rndList.Add(rnd.Next(1, 26);
If you want to make sure they're all unique then you can do something like the code I sent
XLnTz
XLnTzβ€’7mo ago
oh yea, i missed ")" ^^ okay, im gonna try that, the do-/while loop right?
SinFluxx
SinFluxxβ€’7mo ago
yeah that's right So in total your code here:
for (int j = 0; j < 10; j++)
rndList.Add(rnd.Next(1, 26);
for (int j = 0; j < 10; j++)
rndList.Add(rnd.Next(1, 26);
would be replaced with:
int number;
for (int j = 0; j < 10; j++)
{
do {
number = rnd.Next(1, 26);
} while (rndList.Contains(number));
rndList.Add(number);
}
int number;
for (int j = 0; j < 10; j++)
{
do {
number = rnd.Next(1, 26);
} while (rndList.Contains(number));
rndList.Add(number);
}
XLnTz
XLnTzβ€’7mo ago
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? πŸ˜„
SinFluxx
SinFluxxβ€’7mo ago
yeah that's right, rather than creating a list which may contain duplicates and then going through it again afterwards
XLnTz
XLnTzβ€’7mo ago
Oh yeah right, and after that im going to be able to easier compare with users array and add matching numbers to matchNum List? ^^
SinFluxx
SinFluxxβ€’7mo ago
in theory!
XLnTz
XLnTzβ€’7mo ago
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 πŸ™ πŸ™‚
SinFluxx
SinFluxxβ€’7mo ago
No problem, it can be confusing at the start when you're trying to take in a lot of different things
XLnTz
XLnTzβ€’7mo ago
yea, thats for sure xD thanks againπŸ™
Accord
Accordβ€’7mo ago
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.