从数组中获取特定信息
本文关键字:信息 获取 数组 | 更新日期: 2023-09-27 18:02:50
下面是我写的代码。我要做的是输入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();
}
}
}
你应该先得到球员的数量,然后用这个数字创建你的数组。
例如:string[] player = new string[numberOfPlayers];
这也许可以解释为什么你的最低分数总是0。如果您的数组包含空点(如果您输入的球员/分数少于20)。
为了"链接"球员的名字和他的进球,你应该使用Player
类,这样你就可以有一个Player
对象数组,并将其用于得分和名字。然后你会得到这样的结果:
Console.WriteLine("Top player is: {0} with {1} goals", topPlayer.name, topPlayer.score);
使用数组不容易做到这一点。你可以使用字典,它可以让你查找球员的名字并获得进球。事实上,这种值的配对就是字典的作用:
//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
快速浏览你的原始代码-类似这样的东西应该可以工作:
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();
Linq solution:)
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;
您可以使用间接排序使用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();
不幸的是,只有开箱即用的数组支持这一点,而它可能更好地使用列表,只是添加新的球员和分数,直到用户说没有更多的球员可以输入,而不是强迫用户提前输入球员的数量。
其他答案建议使用更面向对象的设计,并将球员和进球组合到一个类中,或者至少使用字典,我真的建议相同,但我不会麻烦重复这里的答案。