0

I am trying to create a program that asks the user "how many player's details would you like to enter?" after they enter this the user is then asked to enter in each of the attributes of these players.

Essentially, I am trying to get the user to instantiate multiple objects of a class.

This is what I have entered into the FootBallPlayer class

class FootballPlayer
{
  private string fullName;
  private int yearBorn;
  private double goalsPerGame;

  // constructor
  public FootballPlayer (string name, int year, double goals)

  {
     fullName = name;
     yearBorn = year;
     goalsPerGame = goals;
  }

  // read-only properties
  public string Name;
  {
     get
       { 
          return fullName;

   public string YearBorn;
  {
     get
       { 
          return yearBorn;
       }
  }

  public string Goals;
  {
     get
       { 
          return goalsPerGame;
       }
  }

In my second class FootballPlayerApp I am trying to get the user to enter firstly the number of players and secondly the details of all of those users.

I have created the following methods GetInput() //which allows the user to enter the number of players and returns it GetName() //which allows the user to enter a players name GetYear() //which allows the user to enter the year born GetGoals() // which allows the user to enter the number of goals scored.

I understand that I can create a single object in the main method as follows

FootballPlayer player1 = new
FootballPlayer ("Lionel Messi", 1988, 2.3);

What I don't understand is

  • how do I have the name of the object (e.g player1 in the example above) be different for each player the user enters when the user is able to enter any number of players.

  • How do I loop it so that multiple players are entered until the numberOfPlayers is reached

  • How do I display the results as follows.

e.g if the user entered 2 players player1 ("Lionel Messi", 1998, 2.3) player2 ("Ronaldo", 1985, 1.4)

how do I get the results to be displayed as

 Player Name    Year Born     Average Goals Scored

 Lionel Messi        1998                      2.3
 Ronaldo             1985                      1.4
1
  • Use a for-loop, create n-instances of FootballPlayer and add them to a List<FootballPlayer>. You can then access each player via index, for example player 10: FootballPlayer playerTen = playerList[9]; Commented Jun 3, 2016 at 15:03

1 Answer 1

2

how do I have the name of the object (e.g player1 in the example above) be different for each player

You don't. Instead of individual variables, consider a collection of objects. Basically, any time you find yourself trying to create object1, object2, object3, and so on then you probably want to use an array or collection of some kind instead.

This could be one of any number of collection types, even a simple array. A common type to use in this case would be List<T>, which for your type would be List<FootballPlayer>.

Structurally it might look something like this:

var players = new List<FootballPlayer>();
var numberOfPlayers = GetInput();
for (var i = 0; i < numberOfPlayers; i++)
{
    // prompt the user to enter the next player

    var player = new FootballPlayer();
    player.Name = GetName();
    player.Goals = GetGoals();
    // etc.  Basically build up a FootballPlayer object from user input

    players.Add(player);
}

The user would be prompted for each player, and you can use the i variable within the loop to provide useful messages. For example, "Enter the details for player 2:" or something like that.

After the loop is finished, you'd have a collection of FootballPlayer objects in the players variable.

Sign up to request clarification or add additional context in comments.

7 Comments

how would I do this using an array?
@firepower4: Generally standard arrays don't allow re-sizing. So you'd need the numberOfPlayers first and then declare the array based on that value. The rest would be very similar though. But instead of "adding" to the array (like one does with a List<T>), you'd be setting the object to an index in the array (which you can identify with the i value within the loop).
yeah that's essentially what I have actually done so far. Created three arrays where the length is equal to the numberOfPlayers. I then used a while statement to get the user to enter info while counter < numberOfPlayers. From info entered assigned that to the appropriate index position in the array. I ended up with 3 arrays where an individual players info is in the same index positions.
@firepower4: Why do you need three arrays? Is the user entering three separate lists of players? The question makes it sound like the user is only entering one list of players.
my three arrays were a string array (which contained the names of all players), a int array (which contained the years born) and a double array (which contained the average goalse). Is it possible to put it all in one array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.