158 lines
4.6 KiB
C#
158 lines
4.6 KiB
C#
namespace Lesson16_List排序练习
|
|
{
|
|
class Monster
|
|
{
|
|
public int atk;
|
|
public int def;
|
|
public int hp;
|
|
public Monster(int atk, int def, int hp)
|
|
{
|
|
this.atk = atk;
|
|
this.def = def;
|
|
this.hp = hp;
|
|
}
|
|
|
|
override public string ToString()
|
|
{
|
|
return $"怪物:atk={atk},def={def},hp={hp}";
|
|
}
|
|
}
|
|
enum E_Quality
|
|
{
|
|
白色 = 0,
|
|
绿色 = 1,
|
|
蓝色 = 2,
|
|
紫色 = 3,
|
|
橘色 = 4
|
|
}
|
|
enum E_ItemType
|
|
{
|
|
武器 = 0,
|
|
护甲 = 1,
|
|
饰品 = 2
|
|
}
|
|
class Item
|
|
{
|
|
public string name;
|
|
public E_Quality quality;
|
|
public E_ItemType itemType;
|
|
|
|
public Item(string name, E_Quality quality, E_ItemType itemType)
|
|
{
|
|
this.name = name;
|
|
this.quality = quality;
|
|
this.itemType = itemType;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"物品:品质={quality},类型={itemType},名字={name}";
|
|
}
|
|
}
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
List<Monster> monsters = new List<Monster>();
|
|
Random r = new Random();
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
monsters.Add(new Monster(r.Next(1, 10), r.Next(1, 10), r.Next(1, 10)));
|
|
Console.WriteLine(monsters[i]);
|
|
}
|
|
Console.WriteLine("_______________________________");
|
|
try
|
|
{
|
|
int inputIndex = int.Parse(Console.ReadLine());
|
|
switch (inputIndex)
|
|
{
|
|
case 1:
|
|
monsters.Sort((m1, m2) => m1.atk - m2.atk);
|
|
break;
|
|
case 2:
|
|
monsters.Sort((m1, m2) => m1.def - m2.def);
|
|
break;
|
|
case 3:
|
|
monsters.Sort((m1, m2) => m1.hp - m2.hp);
|
|
break;
|
|
case 4:
|
|
//反转API
|
|
monsters.Reverse();
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine("ERROR");
|
|
}
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
Console.WriteLine(monsters[i]);
|
|
}
|
|
Console.WriteLine("_______________________________");
|
|
|
|
List<Item> items = new List<Item>();
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
E_Quality randomQuality = (E_Quality)r.Next(0, Enum.GetValues(typeof(E_Quality)).Length);
|
|
E_ItemType randomItemType = (E_ItemType)r.Next(0, Enum.GetValues(typeof(E_ItemType)).Length);
|
|
|
|
items.Add(new Item("Item" + r.Next(100, 201), randomQuality, randomItemType));
|
|
Console.WriteLine(items[i]);
|
|
}
|
|
Console.WriteLine("_______________________________");
|
|
items.Sort((i1, i2) => {
|
|
//先按类型比
|
|
if (i1.itemType != i2.itemType)
|
|
{
|
|
return i1.itemType - i2.itemType;
|
|
}
|
|
//再按品质比
|
|
else if (i1.quality != i2.quality)
|
|
{
|
|
return i2.quality - i1.quality;
|
|
}
|
|
else
|
|
{
|
|
return i1.name.CompareTo(i2.name);
|
|
}
|
|
});
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
Console.WriteLine(items[i]);
|
|
}
|
|
Console.WriteLine("_______________________________");
|
|
|
|
Dictionary<int, string> dic = new Dictionary<int, string>();
|
|
dic.Add(1, "532132");
|
|
dic.Add(3, "532132");
|
|
dic.Add(4, "532132");
|
|
dic.Add(2, "532132");
|
|
dic.Add(6, "532132");
|
|
dic.Add(5, "532132");
|
|
|
|
foreach (var kv in dic)
|
|
{
|
|
Console.WriteLine($"Key={kv.Key},Value={kv.Value}");
|
|
}
|
|
Console.WriteLine("_______________________________");
|
|
List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
|
|
|
|
foreach (KeyValuePair<int,string> item in dic)
|
|
{
|
|
list.Add(item);
|
|
}
|
|
|
|
list.Sort((p1, p2) =>
|
|
{
|
|
return p1.Key - p2.Key;
|
|
});
|
|
|
|
for(int i = 0;i < list.Count;i++)
|
|
{
|
|
Console.WriteLine(list[i].Key + " " + list[i].Value);
|
|
}
|
|
}
|
|
}
|
|
}
|