基本上我必須編寫一個程序,要求用戶列出他們球隊的球員名單,列出他們的名字以及他們得分的進球數。輸出結果應該是「最高的球員是...得分..平均進球數是..最低進球數是..」如何在c#中顯示數組的最大值,最小值和平均值?
我們還沒有真正覆蓋陣列,但我有現在開始了這個問題,它會讓我瘋狂,直到完成。我知道我可能會遠離獲得我所需要的東西,但任何一點正確的方向都會非常感謝! P.S我知道我的代碼的最後一點是完全錯誤的,我只是不知道從哪裏去。我的代碼:
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();
從外觀上來看,它看起來像你已經獲得的最小值和最大值。我猜你的平均水平並沒有給你你所期望的從整數截斷(就是說,給定2個目標和3個目標,你得到2個目標而不是2.5);是你的問題?編輯:沒關係,沒有意識到已經處理了這方面。那麼具體是什麼問題呢? –
你的問題是什麼?我猜你會在'Console.WriteLine'處得到'Exception'? –
什麼是輸入量,平均輸出量,以及您預期的平均值是多少? – TyCobb