Compare()方法,Parent和Children对象排序不完美

发布于 2024-12-14 01:19:17 字数 2416 浏览 0 评论 0原文

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

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

我尝试过编写此方法,但它无法正常工作...

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

或者也许我的Compare方法不完美,也许有错误...我怎样才能瞄准,非AlcoholDrink对象(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();

I have this homework. And I am beginner C# program, and now learn the C# and Java

Homework: Write static method that get a Drink[] array, where the AlcoholDrink has got different alcohol values. The method returns an array containing the three highest alcohol value! If it is not so much AlcoholDrink in the array, the method is returning a null reference!

I have tried write this method, but it not work correctly...

Cause at the sz[1] = (AlcoholDrink)t[1]; it is a Drink object (non-alcohol), I don't understand why it is there...

Or maybe my Compare method is not perfect, maybe there is the mistake... How can I aim, the non AlcoholDrink objects (Drink Objects) go to the end of the array?

Here my C# code:

    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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素染倾城色 2024-12-21 01:19:17

问题在于:

        // 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;

Problem is here:

        // 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;

You want non alcoholic drinks to go the end, but this will return 1 even if one of the drinks is alcoholic. Do this:

else if (x is AlcoholicDrink) return -1;
else if (y is AlcoholicDrink) return 1;
else return 0;
想你只要分分秒秒 2024-12-21 01:19:17

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

您需要显式处理四种不同的情况(AlcoholDrink,AlcoholDrink); (酒精饮料,饮料); (饮料、酒精饮料) 和 (饮料、饮料) 并为每一项返回适当的值。

Suppose the sort function calls your compare method with a normal Drink and an AlcoholDrink. You return 1, which is good because Drink > AlcoholDrink. However if your Compare method is called with an AlcoholDrink and a normal Drink you also return 1, which is bad because AlcoholDrink is not greater than Drink.

You need to explicitly handle the four separate cases (AlcoholDrink,AlcoholDrink); (AlcoholDrink,Drink); (Drink, AlcoholDrink) and (Drink, Drink) and return appropriate values for each one.

情独悲 2024-12-21 01:19:17

您真的需要非酒精饮料才能排到最后吗?你能把它们过滤掉吗?

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

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[];
}

Do you really need the non-alcoholic drinks to go to the end of the array? Could you filter them out instead?

If LINQ is a possibility for you, you could use t.OfType<AlcoholDrink>() to do the filtering. Otherwise, you could implement your own method:

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[];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文