2

我有这个作业。我是初学者C#程序,现在学习C#和Java

作业:编写获取 Drink[] 数组的静态方法,其中 AlcoholDrink 具有不同的酒精值。该方法返回一个包含三个最高酒精值的数组!如果数组中没有那么多酒精饮料,则该方法返回一个空引用!

我试过写这个方法,但它不能正常工作......

原因在 sz[1] = (AlcoholDrink)t[1]; 它是一个饮料对象(非酒精),我不明白为什么它在那里......

或者我的比较方法不完美,可能有错误......我该如何瞄准,非酒精饮料对象(Drink Objects)到数组的末尾?

这是我的 C# 代码:

    class DrinkComparer : IComparer<Drink>
    {
        public int Compare(Drink x, Drink y)
        {
            // AlcoholDrink class is children of Drink class
            if (x is AlcoholDrink && y is AlcoholDrink)
            {
                AlcoholDrink a = (AlcoholDrink)x;
                AlcoholDrink b = (AlcoholDrink)y;
                double aAlk = a.GetValueOfAlcohol; 
                double bAlk = b.GetValueOfAlcohol;
                if (aAlk > bAlk)
                    return -1;
                else if (aAlk < bAlk)
                    return 1;
                else
                    return 0;
            }
            // Drink objects haven't got GetValueOfAlcohol method...
            // How can I aim, the non AlcoholDrink objects (Drink objects) go to the end     array?
            else
                return 1;                
        }
    }
    static AlcoholDrink[] Largest3AlcoholDrink(Drink[] t)
    {
        Array.Sort(t, new DrinkComparer());
        AlcoholDrink[] sz = new AlcoholDrink[3];
        sz[0] = (AlcoholDrink)t[0];
        sz[1] = (AlcoholDrink)t[1];
        sz[2] = (AlcoholDrink)t[2];
        return sz;
    }
        AlcoholDrink sz = new AlcoholDrink( "Kékfrankos" , "0.75 l", 1500, 4.5);
        Console.WriteLine(sz);

        Drink[] t = new Drink[8];
        t[0] = new AlcoholDrink("Kék Portói", "0.75 l", 1200, 20.5);
        t[1] = new Drink("Tocsik", "0.75 l", 1100); // Non Alcohol Drink
        t[2] = new AlcoholDrink("Tokaji Asszú", "0.75 l ", 1600, 14.5);
        t[3] = new AlcoholDrink("Egri Bikavér", "0.75 l", 1500, 23.5);
        t[4] = new Drink("Egri Szamóca", "0.75 l", 1100); // Non Alchol Drink
        t[5] = new AlcoholDrink("Egri Merlot", "0.75 l", 1700, 18.5);
        t[6] = new AlcoholDrink("Egri Medina", "0.75 l", 900, 16.5);
        t[7] = new AlcoholDrink("Törley Talisman", "0.75 l", 750, 4.5);
        Console.WriteLine(DrinkKeres( t, "Egri Bikavér"));

        Largest3AlcoholDrink(t);

        Console.ReadLine();
4

3 回答 3

2

问题在这里:

        // Drink objects haven't got GetValueOfAlcohol method...
        // How can I aim, the non AlcoholDrink objects (Drink objects) go to the end     array?
        else
            return 1;

您希望非酒精饮料结束,但即使其中一种饮料是酒精的,这也会返回 1。做这个:

else if (x is AlcoholicDrink) return -1;
else if (y is AlcoholicDrink) return 1;
else return 0;
于 2011-11-05T20:51:18.657 回答
0

假设 sort 函数使用普通的 Drink 和 AlcoholDrink 调用您的比较方法。您返回 1,这很好,因为 Drink > AlcoholDrink。但是,如果使用 AlcoholDrink 和普通 Drink 调用 Compare 方法,您也会返回 1,这很糟糕,因为 AlcoholDrink 不大于 Drink。

您需要明确处理四种不同的情况(AlcoholDrink,AlcoholDrink);(酒精饮料,饮料);(Drink, AlcoholDrink) 和 (Drink, Drink) 并为每个返回适当的值。

于 2011-11-05T20:48:41.127 回答
0

你真的需要非酒精饮料去阵列的尽头吗?你能把它们过滤掉吗?

如果您可以使用 LINQ,则可以使用它t.OfType<AlcoholDrink>()来进行过滤。否则,您可以实现自己的方法:

public AlcoholDrink[] GetAlcoholicDrinks(Drink[] drinks) {
  ArrayList alcoholDrinks = new ArrayList();
  foreach (Drink drink in drinks) {
    if (drink is AlcoholDrink) {
      alcoholDrinks.Add(drink);
    }
  }
  return alcoholDrinks.ToArray(typeof(AlcoholDrink)) as AlcoholDrink[];
}
于 2011-11-05T21:00:13.077 回答