Reputation: 1
Basically I have to write a program that asks a user to name the amount of players on their football team, list their names and how many goals they have scored. The output should then be "the top player is..with a score of .. The average goals scored is.. The lowest goals scored is.."
We haven't really covered arrays properly yet, but I have started this question now and it will drive me crazy until I get it done. I know I am probably a good bit away from getting what I need but any point in the right direction would be really appreciated! P.S I know the last bit of my code is completely wrong I just don't know where to go from here. My code:
Console.WriteLine("Enter the amount of players");
int amount = int.Parse(Console.ReadLine());
string[] names = new string[amount];
int[] playerGoals = new int[amount];
int result;
string playerName;
for (int i = 0; i < playerGoals.Length; i++)
{
Console.WriteLine("Enter a players name");
playerName = Console.ReadLine();
names[i] = playerName;
Console.WriteLine("Enter how many goals they have score this season");
result = int.Parse(Console.ReadLine());
playerGoals[i] = result;
}
int minimum = playerGoals.Min();
int maximum = playerGoals.Max();
double average = playerGoals.Average();
Console.WriteLine("The top player is {0} with a score of {1}", maximum);
Console.WriteLine("");
Console.WriteLine("The average goals scored is {0}", average);
Console.WriteLine("");
Console.WriteLine("The lowest goal scored is {1}");
Console.ReadLine();
Upvotes: 0
Views: 1561
Reputation: 56536
Here are some approaches you could take:
Look up the player with the max score
string maxPlayer = names[Array.IndexOf(playerGoals, maximum)];
Calculate the max yourself in a loop (either as you're taking the inputs or afterward), in a way that you keep track of the player along with it.
Create a PlayerStats
class so you have one array (PlayerStats[]
) instead of two, and use MoreLINQ's MaxBy
. This would end up with the best code in my opinion, but is possibly more advanced than you're ready for (knowing how to do things manually is a good skill to have, although you don't always use it in the real world).
var best = playerStats.MaxBy(x => x.Goals);
Console.WriteLine("The top player is {0} with a score of {1}",
best.Name, best.Goals);
public class PlayerStats
{
public string Name { get; set; }
public int Goals { get; set; }
}
Upvotes: 2
Reputation: 222582
class Player
{
public string Name { get; set; }
public int goals { get; set; }
}
static void Main(string[] args)
{
Console.WriteLine("Enter the amount of players");
int amount = int.Parse(Console.ReadLine());
List<Player> _players = new List<Player>();
for (int i = 0; i < amount; i++)
{
Player objPlayer = new Player();
Console.WriteLine("Enter a players name");
objPlayer.Name = Console.ReadLine();
Console.WriteLine("Enter how many goals they have score this season");
objPlayer.goals = int.Parse(Console.ReadLine());
_players.Add(objPlayer);
}
int maxgoals = _players.Max(t => t.goals);
var maxplayer = _players.FirstOrDefault(t => t.goals == maxgoals).Name;
int mingoals = _players.Min(t => t.goals);
var minplayer = _players.FirstOrDefault(t => t.goals == maxgoals).Name;
var average = _players.Sum(t=>t.goals)/amount;
Console.WriteLine("The top player is {0} with a score of {1}", maxplayer, maxgoals);
Console.WriteLine("");
Console.WriteLine("The bottom player is {0} with a score of {1}", minplayer, mingoals);
Console.WriteLine("");
Console.WriteLine("The average goals scored is {0}", average);
Console.ReadLine();
}
Upvotes: 0