2014-05-13 177 views
0

基本上我必須編寫一個程序,要求用戶列出他們球隊的球員名單,列出他們的名字以及他們得分的進球數。輸出結果應該是「最高的球員是...得分..平均進球數是..最低進球數是..」如何在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(); 
+0

從外觀上來看,它看起來像你已經獲得的最小值和最大值。我猜你的平均水平並沒有給你你所期望的從整數截斷(就是說,給定2個目標和3個目標,你得到2個目標而不是2.5);是你的問題?編輯:沒關係,沒有意識到已經處理了這方面。那麼具體是什麼問題呢? –

+1

你的問題是什麼?我猜你會在'Console.WriteLine'處得到'Exception'? –

+0

什麼是輸入量,平均輸出量,以及您預期的平均值是多少? – TyCobb

回答

2

這裏有一些方法,你可以採取:

  1. 查找與最大的球員得分

    string maxPlayer = names[Array.IndexOf(playerGoals, maximum)]; 
    
  2. 自己計算最大的循環(或者因爲你正在接受輸入或之後),以一種方式跟蹤玩家。

  3. 創建一個PlayerStats類,以便您有一個數組(PlayerStats[])而不是兩個,並使用MoreLINQ'sMaxBy。這將以我認爲最好的代碼結束,但可能比您準備好的更先進(知道如何手動執行操作是一項很好的技能,儘管您並不總是在真實世界中使用它) 。

    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; } 
    } 
    
+0

@Downvoters:關心評論? –

+0

有人很無聊,並且對每個答案都讚不絕口...... –

+0

感謝你們的幫助,這有點讓人接受,但我會在下一段時間與它一起,並嘗試理解它!再次感謝 – user3634115

0
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(); 
     } 
+0

downvotes的任何理由? – Sajeetharan

+1

誰投下了你的答案,甚至沒有時間閱讀你的答案大聲笑。有人確實很難過。 –

相關問題