2014-12-08 29 views
0

我需要找到一個客戶來排序名稱和分數從最高到最低。我不確定如何使用交換方法,並且如果有人能幫助讓我看到方式,我會很喜歡它。另外,有沒有辦法顯示分數,讓它們排列整齊一些?如何使用交換方法將數組從高到低排序?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace proj09LEA 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // declare and array of integers   
      string[] name = new string[5]; 
      int[] score = new int[5]; 

      Console.WriteLine("\nSaturday Coder's Bowling Team"); 
      Console.WriteLine("Enter in a name and score for each person on the team."); 
      Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.\n"); 

      // fill an array with user input 
      for (int i = 0; i < score.Length; i++) 
      { 
       Console.WriteLine("Enter in a name and score: "); 
       //save the name and score as a string 
       string line = Console.ReadLine(); 

       //split the name and score 
       name[i] = line.Substring(0, line.IndexOf(' ')); 
       score[i] = int.Parse(line.Substring(line.IndexOf(' ') + 1)); 
      } 

      Console.WriteLine("------------ Input Complete ------------\n"); 
      Console.WriteLine("Here are the scores for this game, from highest to lowest:\n"); 

      for (int i = 0; i < score.Length; i++) 
      { 
       if (score[i] >= 300) 
       { 
        Console.WriteLine("{0}  {1}*.", name[i], score[i]); 
       } 
       else 
       { 
        Console.WriteLine("{0}  {1}", name[i], score[i]); 
       } 
      } 

      //display the average score in the program 
      AverageScore(score); 

      Console.WriteLine("Press Enter to continue. . ."); 
      //end program 
      Console.ReadLine(); 
     } 

     // find the average score from the score array 
     static void AverageScore(int[] score) 
     { 
      int sum = score.Sum(); 
      int average = sum/score.Length; 
      Console.WriteLine("\nThe average score for this game was {0:D}.\n", average); 
     } 
    } 
} 
+0

需要實現一種方法將測試對方的分數條目,一旦你發現某些東西是更大或更小的交換這些元素與另一種方法... – Ubica 2014-12-08 02:15:00

回答

0

不要爲您的名字和分數保留單獨的數組。用Name和Score屬性創建一個類。在類中實現IComparable,並且可以在類中使用內置的List.Sort。

public class BowlingScore : IComparable<BowlingScore> 
{ 
    private int _score = 0; 

    public string Name { get; set; } 

    public bool IsPerfectGame { get; protected set; } 

    public int Score 
    { 
     get { return this._score; } 

     set { this._score = value; this.IsPerfectGame = value == 300; } 
    } 

    public override string ToString() 
    { 
     if (this.IsPerfectGame) 
     { 
      return string.Format("{0}'s score was *{1}", this.Name, this.Score); 
     } 
     else 
     { 
      return string.Format("{0}'s score was {1}", this.Name, this.Score); 

     } 
    } 

    public int CompareTo(BowlingScore other) 
    { 
     if (other == null) return 1; 

     return -1 * this.Score.CompareTo(other.Score); 
    } 

此代碼將前高後低收集姓名和分數從控制檯,排序和打印出來的分數,並打印出的高,低和平均成績:

 List<BowlingScore> scores = new List<BowlingScore>(5); 

     for (int index = 0; index < 5; index++) 
     { 
      Console.WriteLine("Enter in a name and score: "); 
      string line = Console.ReadLine(); 

      BowlingScore bowlingScore = new BowlingScore(); 
      bowlingScore.Name = line.Substring(0, line.IndexOf(' ')); 
      bowlingScore.Score = int.Parse(line.Substring(line.IndexOf(' ') + 1)); 

      scores.Add(bowlingScore); 
     } 

     Console.WriteLine(); 
     Console.WriteLine("Here are the Scores:"); 

     scores.Sort(); 

     foreach (BowlingScore score in scores) 
     { 
      Console.WriteLine(score); 
     } 

     Console.WriteLine(); 
     HighScores(scores); 

     Console.WriteLine(); 
     LowScores(scores); 

     Console.WriteLine(); 
     Console.WriteLine(string.Format("Average score:{0}", scores.Average(f => f.Score))); 

     Console.WriteLine(); 
     Console.WriteLine("Press any key..."); 
     Console.ReadKey(); 

    static void LowScores(List<BowlingScore> scores) 
    { 
     int highScore = scores.ElementAt(scores.Count - 1).Score; 

     for (int index = scores.Count - 1; index > 0; index--) 
     { 
      BowlingScore bowlingScore = scores.ElementAt(index); 

      if (bowlingScore.Score == highScore) 
      { 
       Console.WriteLine(string.Format("Low Score: {0} {1}", bowlingScore.Name, bowlingScore.Score)); 
      } 
      else 
      { 
       break; 
      } 
     } 
    } 

    static void HighScores(List<BowlingScore> scores) 
    { 
     int lowScore = scores.ElementAt(0).Score; 

     for (int index = 0; index < scores.Count; index++) 
     { 
      BowlingScore bowlingScore = scores.ElementAt(index); 

      if (bowlingScore.Score == lowScore) 
      { 
       Console.WriteLine(string.Format("High Score: {0} {1}", bowlingScore.Name, bowlingScore.Score)); 
      } 
      else 
      { 
       break; 
      } 
     } 
    }