Compare()方法,Parent和Children对象排序不完美
我有这个作业。我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于:
您希望非酒精饮料放在最后,但即使其中一种饮料是酒精饮料,这也会返回 1。这样做:
Problem is here:
You want non alcoholic drinks to go the end, but this will return 1 even if one of the drinks is alcoholic. Do this:
假设排序函数调用您的比较方法与普通饮料和酒精饮料。您返回 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.
您真的需要非酒精饮料才能排到最后吗?你能把它们过滤掉吗?
如果 LINQ 适合您,您可以使用 t.OfType() 进行过滤。否则,您可以实现自己的方法:
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: