2013-01-03 15 views
22
using System; 
using System.Xml; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      SortedSet<Player> PlayerList = new SortedSet<Player>(); 

      while (true) 
      { 
       string Input; 
       Console.WriteLine("What would you like to do?"); 
       Console.WriteLine("1. Create new player and score."); 
       Console.WriteLine("2. Display Highscores."); 
       Console.WriteLine("3. Write out to XML file."); 
       Console.Write("Input Number: "); 
       Input = Console.ReadLine(); 
       if (Input == "1") 
       { 
        Player player = new Player(); 
        string PlayerName; 
        string Score; 

        Console.WriteLine(); 
        Console.WriteLine("-=CREATE NEW PLAYER=-"); 
        Console.Write("Player name: "); 
        PlayerName = Console.ReadLine(); 
        Console.Write("Player score: "); 
        Score = Console.ReadLine(); 

        player.Name = PlayerName; 
        player.Score = Convert.ToInt32(Score); 

        //==================================== 
        //ERROR OCCURS HERE 
        //==================================== 
        PlayerList.Add(player); 


        Console.WriteLine("Player \"" + player.Name + "\" with the score of \"" + player.Score + "\" has been created successfully!"); 
        Console.WriteLine(); 
       } 
       else 
       { 
        Console.WriteLine("INVALID INPUT"); 
       } 
      } 
     } 
    } 
} 

を実装する必要があります。だから私は、第二のプレーヤーを追加しようとすると、最初のものは動作しますが、もう一つはない「少なくとも1つの目的は、IComparableをに

At least one object must implement IComparable.

」を得続けます。 SortedSetも使用しなければなりません。なぜなら、それは仕事のための要件なので、学校の仕事です。

+6

あなたは知っておくべきことを正確に伝えています - あなたの 'Player'クラスは' IComparable'インターフェースを実装しなければなりません –

+1

この種のエラーは存在してはいけません、理由のために良い型保証言語を持っています、 'SortedSet 'は本当に' T:IComparable ' – Lukazoid

答えて

48

さて、あなたは順序を気を意味している... SortedSet<>を使用しようとしている。.. IComparableインターフェイスを実装する必要があります。しかし、あなたのPlayerタイプの音は、IComparable<Player>を実装していません。だから、どのような並べ替え順序が見えますか?

基本的には、あなたのPlayerコードに、あるプレーヤーと他のプレーヤーを比較する方法を伝える必要があります。また、あなたはどこか別の場所IComparer<Player>を実装し、あなたが選手をしたいどのような順序を示すためにSortedSet<>のコンストラクタにその比較を渡すことができますたとえば、あなたが持っている可能性が:。その後

public class PlayerNameComparer : IComparer<Player> 
{ 
    public int Compare(Player x, Player y) 
    { 
     // TODO: Handle x or y being null, or them not having names 
     return x.Name.CompareTo(y.Name); 
    } 
} 

// Note name change to follow conventions, and also to remove the 
// implication that it's a list when it's actually a set... 
SortedSet<Player> players = new SortedSet<Player>(new PlayerNameComparer()); 
+11

+1これは実際にOPを説明する唯一の答えです**なぜ彼はIComparable – ken2k

+0

yuppありがとう、私はこれを試していますが、私は各プレーヤーのスコアを比較しようとすると、それを整えようとしています最高のスコアが最初にやって来るように。 しかし、イムこのエラーを取得: エラー一貫性のないアクセス:パラメータタイプ 'ConsoleApplication1.Playerが' メソッドよりも少ないアクセス可能である「ConsoleApplication1.Comp.Compare(ConsoleApplication1.Player、ConsoleApplication1.Player) – user1930824

+0

user1930824 @:右、だからそれを公開クラスではなく内部クラスにする必要があります。次に、名前ではなくスコアを比較します。 (私はあなたが必要とする正確な*コードをスプーンフィードするつもりはありません...) –

1

PlayerクラスをIComparableに設定してください。

+0

という制約を持っていなければなりません。 – Jogi

0

PlayerクラスでIComparableインターフェイスを実装する必要があります。 SortedSetはソートされた順序でアイテムを保持しますが、ソートされた順序がどのようにソートされているのか(IComparableを使用して)ソートされた順序がどのようなものであるかはどのように分かりますか?

関連する問題