While I wait for my programming education to begin (.NET / C #), I practice myself in the meantime. I'm still a Padawan coder.
Right now I am coding a small lottery game that should work something like this:
- Enter name and 7 numbers between 1-35.
- Once you have entered all the players and their numbers, just press ENTER to continue.
- Generate the correct lottery line (11 numbers in total)
- Data are sorted in numerical order.
- Data is corrected.
- Data is presented as the example below:
Correct lottery line: 7,8,9,10,11,12,15
Additional numbers: 21,22,23,24
James: 1,4,6,7,8,20,21 2 correct, 1 additional number
Jane: 1,6,8,12,14,15,35 3 correct, zero additional number*
- Data is saved in a file on the hard disk
The problem right now is that the foreach loop outputs the same (the last entered) lottery number to ALL players. Cannot figure out why, thus I need some advice. See below code.
internal class Program
{
static void Main(string[] args)
{
string nameInput = null;
List<Player> listOfPlayers = new List<Player>();
int[] numberInput = new int[7];
while (nameInput != "")
{
Console.Write("Type in your name or press ENTER to continue."
"\nName: ");
nameInput = Console.ReadLine();
if (nameInput != "")
{
try
{
for (int i = 0; i < 7; i )
{
Console.Write($"Type in lotto nr{i 1}: ");
numberInput[i] = int.Parse(Console.ReadLine());
}
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
listOfPlayers.Add(new Player(nameInput, numberInput));
}
}
foreach (Player player in listOfPlayers)
{
Console.WriteLine("{0} {1}", player.Name, string.Join(", ", player.Numbers));
}
Console.ReadLine();
}
}
public class Player
{
public string Name { get; set; }
public int[] Numbers { get; set; }
public Player(string name, int[] numbers)
{
Name = name;
Numbers = numbers;
}
}
CodePudding user response:
You need to move numberInput into the loop as in the code below.
As a bonus, i did apply some of your upcoming features to help a little :)
internal class Program
{
static List<Player> listOfPlayers = new List<Player>();
static void RunGame()
{
Console.Write("Type in your name or press ENTER to continue."
"\nName: ");
// Wait for name input:
string nameInput = Console.ReadLine();
if (!string.IsNullOrEmpty(nameInput))
{
int[] numberInput = new int[7];
for (int i = 0; i < 7; i )
{
// Use this bool to keep looping until user input correct number:
bool moveon = false;
do
{
Console.Write($"Type in lotto nr {i 1}: ");
string input = Console.ReadLine();
// First check if input is a number, next check that input is between 1 and 35.
// The function TryParse takes the string input and tries to parse it as an int.
// If it's parsed, it's returned as a new int 'useinput':
if (int.TryParse(input, out int useinput) && useinput >= 1 && useinput <= 35)
{
// Add number to int[]:
numberInput[i] = useinput;
// Allow user to skip loop and enter new number.
// 'Continue' breaks directly out of loop:
moveon = true; continue;
}
else { Console.WriteLine($"Error: Input a number between 1 and 35:"); }
}
// Keep looping until moveon = true:
while (moveon == false);
}
// Sort every input in numberInput ascending:
Array.Sort(numberInput);
// Add name and sorted numbers to list of players:
listOfPlayers.Add(new Player(nameInput, numberInput));
}
else
{
// List players and their lottery numbers:
foreach (Player player in listOfPlayers)
{
Console.WriteLine("{0} {1}", player.Name, string.Join(", ", player.Numbers));
}
}
// Start over again.
RunGame();
}
static void Main(string[] args) { RunGame(); }
public class Player
{
public string Name { get; set; }
public int[] Numbers { get; set; }
public Player(string name, int[] numbers)
{
Name = name;
Numbers = numbers;
}
}
}
}
CodePudding user response:
When you give the numberInput
array to the Player
variable
here:
listOfPlayers.Add(new Player(nameInput, numberInput));
you actually give a reference to the same array. So basically, all your players have a reference to the same array in memory.
What you want to do is register a copy of this array for each player.
You can do the copy easyly with .Clone()
and casting back to int[] :
//old code
listOfPlayers.Add(new Player(nameInput, numberInput));`
//new code
listOfPlayers.Add(new Player(nameInput, (int[])numberInput.Clone()));`