本文介绍了C#如何分拣食物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个类Food,其中的字段是`Name,Category,Price和toString Function。 我在Main中创建了Food的数组。例如` 食物[] f = 新食物[ 5 ] { new 食物( Apple , Fruit, 200 ), new 食物( Orange, Fruit, 600 ), new 食物( 番茄 , 蔬菜, 100 ), new 食物( Pork, 肉, 2000 ), new 食物( Rice, Grain, 400 )}; 然后在Console中要求单击1,2或3如果我将点击1,它将对我的数组进行排序根据名称,如果我将点击2,它将按照类别对我的数组进行排序,如果我将点击3,它将根据Price排序我的数组。例如i单击3它将显示如下这样` new 食物( 番茄, 蔬菜, 100 ), new 食物( Apple , Fruit, 200 ), new 食物( Rice, Grain, 400 ), new 食物( 橙色, 水果, 600 ), new 食物( Pork, 肉, 2000 ), 排序后,再次进行民意调查,帮助While Loop 我尝试过: class 食物 { public 字符串名称; public string 类别; public int 价格; public 食物( string a, string b, int c) { this .name = a ; 此 .category = b; this .price = c; } public string toString() { return 此 .name + + this .category + + this .price; } } class 排序:IComparer< food> { public int 比较(食品x,食品y) { double d1 = x.price; double d2 = y.price; 返回 d1 > d2? 1 : - 1; // 对于双 string s1 = x.name; string s2 = y.name; return String .Compare(x.name,y.name); // 对于字符串 } } 静态 void Main( string [] args) { Food [] f = new 食物[ 5 ] { new 食物( Apple, Fruit, 200 ), new 食物( Orange, Fruit, 600 ), new 食物( Tomato, 蔬菜, 100 ), new 食物( Pork, 肉, 2000 ), new 食物( Rice, Grain, 400 )}; foreach (食物项 in f) { Console。的WriteLine(item.toString()); } int x; while ( true ) { Console.WriteLine( \ n \ n 1.根据名称\ n 2.根据类别\ n 3.根据价); Console.Write( \ n输入您的选择:); x = int .Parse(Console.ReadLine()); Array.Sort(f, new Sorting()); foreach (食物项 in f) { Console。的WriteLine(item.toString()); } } } 解决方案 使用调试器逐步执行代码你会看到你的比较课程中发生了什么。它总是价格比较,因为它们是前三行,当你返回时,函数存在,所以它永远不会到达它之后的代码。 你'我需要为你想要做的每种类型的排序使用不同的分拣机 class PriceSorting:IComparer< Food> { public int Compare(食品x,食品y) { double d1 = x.price; double d2 = y.price; 返回d1> d2? 1:-1; //对于double } } class NameSorting:IComparer< Food> { public int Compare(食品x,食品y) { string s1 = x.name; string s2 = y.name; 返回String.Compare(x.name,y.name); //对于字符串} } 然后决定根据他们使用哪一个选择 x = int.Parse(Console.ReadLine()); switch(x) { case 1: Array.Sort(f,new NameSorting()); 休息; case 3: Array.Sort(f,new PriceSorting()); 休息; } 您可以使用 Linq [ ^ ]方法: 1. Enumerable.OrderBy( TSource,TKey)方法(IEnumerable(TSource),Func(TSource,TKey))(System.Linq) [ ^ ] 2. Enumerable.OrderByDescending Method(System.Linq) [ ^ ] 3. 可数.ThenBy方法(System.Linq) [ ^ ] 4. Enumerable.ThenByDescending Method(System.Linq) [ ^ ] 例如: // get按类别和名称排序的食品 var FoodsByCategoryAndName = f.OrderBy(x => x.category).ThenBy(x => x.name); // 按类别和价格(降序)获取食品 var FoodsByCategoryAndPrice = f.OrderBy(x => x.category).ThenByDescending(x => x.price); I have a class Food which fields is` Name,Category,Price and toString Function.I created Food's array in Main.For Example`Food[] f = new Food[5] { new Food("Apple","Fruit",200), new Food("Orange","Fruit",600), new Food("Tomato","Vegetable",100), new Food("Pork","Meat",2000), new Food("Rice","Grain",400) };Then in Console asked to click 1,2 or 3.If i will click 1, it will sorting my array According to name,if i will click 2, it will sorting my array According to Category, and if i will click 3, it will sorting my array According to Price.For Example i Clicked 3 it will show like this`new Food("Tomato","Vegetable",100),new Food("Apple","Fruit",200),new Food("Rice","Grain",400),new Food("Orange","Fruit",600),new Food("Pork","Meat",2000),After sorting, a poll is done again՝ with helping While LoopWhat I have tried:class Food { public string name; public string category; public int price; public Food(string a,string b,int c) { this.name = a; this.category = b; this.price = c; } public string toString() { return this.name + " " + this.category + " " + this.price; }}class Sorting : IComparer<food> { public int Compare(Food x, Food y) { double d1 = x.price; double d2 = y.price; return d1 > d2 ? 1 : -1; //For double string s1 = x.name; string s2 = y.name; return String.Compare(x.name,y.name); // For string } }static void Main(string[] args) { Food[] f = new Food[5] { new Food("Apple","Fruit",200), new Food("Orange","Fruit",600), new Food("Tomato","Vegetable",100), new Food("Pork","Meat",2000), new Food("Rice","Grain",400) }; foreach (Food item in f) { Console.WriteLine(item.toString()); } int x; while (true) { Console.WriteLine("\n\n 1.According to name\n 2.According to category \n 3.According to price"); Console.Write("\n Enter your choice: "); x = int.Parse(Console.ReadLine()); Array.Sort(f, new Sorting()); foreach (Food item in f) { Console.WriteLine(item.toString()); } } } 解决方案 Step through the code using the debugger and you'll see what is happening inside your compare class. It always does the price compare because they are the first three lines and when you "return" the function exists so it will never reach the code after it.You'll need a different sorter for each type of sorting you want to doclass PriceSorting : IComparer<Food>{ public int Compare(Food x, Food y) { double d1 = x.price; double d2 = y.price; return d1 > d2 ? 1 : -1; //For double }}class NameSorting : IComparer<Food>{ public int Compare(Food x, Food y) { string s1 = x.name; string s2 = y.name; return String.Compare(x.name, y.name); // For string }}then decide which one to use based on their selectionx = int.Parse(Console.ReadLine());switch(x){ case 1: Array.Sort(f, new NameSorting()); break; case 3: Array.Sort(f, new PriceSorting()); break;}You can use Linq[^] methods:1. Enumerable.OrderBy(TSource, TKey) Method (IEnumerable(TSource), Func(TSource, TKey)) (System.Linq)[^] 2. Enumerable.OrderByDescending Method (System.Linq)[^] 3. Enumerable.ThenBy Method (System.Linq)[^] 4. Enumerable.ThenByDescending Method (System.Linq)[^] For example://get foods ordered by Category and Namevar FoodsByCategoryAndName = f.OrderBy(x=>x.category).ThenBy(x=>x.name);//get foods ordered by Category and Price (descending)var FoodsByCategoryAndPrice = f.OrderBy(x=>x.category).ThenByDescending(x=>x.price); 这篇关于C#如何分拣食物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 20:41