54
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 を実装する必要があります。

" 2 番目のプレーヤーを追加しようとすると、最初のプレーヤーは機能しますが、2 番目のプレーヤーは機能しません。SortedSetそれが作業の要件であるため、私も使用する必要があります。これは学校の作業です。

4

6 に答える 6

92

さて、あなたは使用しようとしていますSortedSet<>...これは、順序を気にすることを意味します. しかし、あなたの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());
于 2013-01-03T15:29:13.320 に答える
1

Player クラスは IComparable インターフェイスを実装する必要があります。

http://msdn.microsoft.com/en-gb/library/system.icomparable.aspx

于 2013-01-03T15:28:32.270 に答える
0

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

于 2013-01-03T15:30:44.743 に答える
-1

Playerクラスに を実装させますIComparable

于 2013-01-03T15:28:19.130 に答える