以下是我编写的代码。我要做的就是输入3个球员的名字和他们得分的进球数。然后应用程序应输出具有最多目标和目标数量的玩家名称。此外,应用程序应输出得分最低的目标数和得分的平均目标。我可以获得最大进球数,但我无法弄清楚如何将球员名称与得分进球联系起来。此外,当我尝试获得最低目标得分时,我一直得到0值。感谢您的帮助。
{
string []player = new string[20];
double [] goalsScored = new double[20];
int numberOfPlayers;
Console.WriteLine("Enter the number of players:");
numberOfPlayers = int.Parse(Console.ReadLine());
for (int i = 0; i < numberOfPlayers; i++)
{
Console.WriteLine("Enter a players name:");
player[i]= Console.ReadLine();
Console.WriteLine("Goals scored so far this season");
goalsScored [i] = int.Parse(Console.ReadLine());
}
double max = goalsScored.Max();
double sum = goalsScored.Sum();
double average = sum / numberOfPlayers;
Console.WriteLine("Top player is: {0} with {1} goals", player, max);
Console.WriteLine("The average goals scored was:{0}",average);
double min = goalsScored.Min();
Console.WriteLine("The lowest goals scored was: {0}", min);
Console.ReadLine();
}
}
}
答案 0 :(得分:0)
Linq解决方案:)
var players = player.Zip(goalsScored, (n,g) => new { Name = n, Goals = g });
var topGoalscorer = players.OrderByDescending(p => p.Goals).First();
var misser = players.OrderBy(p => p.Goals).First();
topGoalscorer.Goals // max goals
topGoalscorer.Name // player name
使用目标值:
string playerName = player[Array.IndexOf(goalsScored, min)];
替代解决方案:
int maxScoredGoals = -1;
string topGoalscorer = "";
for(int i = 0; i < numberOfPlayes; i++)
{
if (goalsScored[i] <= maxScoredGoals)
continue;
maxScoredGoals = goalsScored[i];
topGoalsocrer = player[i];
}
建议:使用单一数据结构来保存相关数据,即玩家姓名和得分目标
public class Player
{
public string Name { get; set; }
public int Goals { get; set; }
}
也使用泛型集合而不是数组:
List<Player> players = new List<Player>();
填充集合:
for(int i = 0; i < numberOfPlayers; i++)
{
var player = new Player();
player.Name = Console.ReadLine();
player.Goals = Int32.Parse(Console.ReadLine());
players.Add(player);
}
获得最佳射手:
var topGoalscorer = players.OrderByDescending(p => p.Goals).First().Name;
或使用MoreLINQ:
var topGoalscorer = players.MaxBy(p => p.Goals).Name;
答案 1 :(得分:0)
您应首先获得玩家数量,然后使用该数字创建阵列。
例如:
string[] player = new string[numberOfPlayers];
这可以解释为什么你总是将零作为最低分数。如果您的阵列包含空位(如果您输入的球员/得分少于20)。
To&#34; link&#34;你的目标得分的玩家的名字,你应该真的使用Player
类,这样你就可以拥有一个Player
个数组,并将它用于得分和名称。那么你最终得到的结论是:
Console.WriteLine("Top player is: {0} with {1} goals", topPlayer.name, topPlayer.score);
答案 2 :(得分:0)
使用数组无法轻松完成此操作。您可以使用词典,这将允许您查找玩家的名字并获得目标。事实上,这种价值观配对是字典的用途:
//declare a dictionary string, int. The "string" is the name which we look up to get the
//second data point of "int", which will be the number of goals
Dictionary<string, int> playerGoals = new Dictionary<string, int>();
//add a couple of players, and their goal tally
playerGoals.Add("Gianfranco Zola", 44);
playerGoals.Add("Wayne Rooney", 77);
//use the string name to retrieve the int value
int rooneyGoals = playerGoals["Wayne Rooney"];
//rooneyGoals is now 77
Quicky鞭打原始代码 - 这样的事情应该有效:
Dictionary<string, int> playerGoals = new Dictionarty<string, int>()'
int numberOfPlayers;
Console.WriteLine("Enter the number of players:");
numberOfPlayers = int.Parse(Console.ReadLine());
for (int i = 0; i < numberOfPlayers; i++)
{
Console.WriteLine("Enter a players name:");
string playerName = Console.ReadLine();
Console.WriteLine("Goals scored so far this season");
int goalsScored = int.Parse(Console.ReadLine());
playerGoals.Add(playerName, goalsScored);
}
double max = playerGoals.Values.Max();
double sum = playerGoals.Values.Sum();
double average = sum / numberOfPlayers;
Console.WriteLine("Top player is: {0} with {1} goals", player, max);
Console.WriteLine("The average goals scored was:{0}",average);
double min = playerGoals.Values.Min();
Console.WriteLine("The lowest goals scored was: {0}", min);
Console.ReadLine();
答案 3 :(得分:0)
您可以使用Array.Sort(goalsScored, players);
进行间接排序,以便在排序数组时保持第二个数组同步。其他问题在以下代码的注释中得到解决。
var numberOfPlayers = Int32.Parse(Console.ReadLine());
// Allocate the arrays with the correct size; in your code numberOfPlayers
// larger than 20 will cause trouble.
var players = new String[numberOfPlayers];
var goalsScored = new Double[numberOfPlayers];
// Fill the arrays as in your code.
// This will sort the goalsScored array and simultaneously
// bring the players array into the same order; that is called
// indirect sorting.
Array.Sort(goalsScored, players);
// The maximum number of goals scored and the associated player
// are now in the last array position.
var topPlayer = players.Last();
var maxNumberGoals = goalsScored.Last();
// You always got 0 because you always allocated arrays of size
// 20 and if you have less than 20 players all the superfluous
// entries in the array just have their default value of 0 making
// the minimum in the array 0.
var minNumberGoals = goalsScored.First();
var totalNumberGoals = goalsScored.Sum();
var averageNumberGoals = goalsScored.Average();
不幸的是,对于阵列只有开箱即用的支持,而使用列表可能更好,只需添加新玩家和分数,直到用户说没有更多的玩家输入而不是强迫用户提前输入玩家数量。
其他答案建议使用更加面向对象的设计,并将玩家和得分的目标组合成一个班级或者至少使用字典,我真的建议相同,但我不打算在这里复制答案。