试一试:
static void Main(string[] args)
{
Dictionary<string, string[]> skins = new Dictionary<string, string[]>();
skins.Add("ahri", new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
skins.Add("leesin", new string[] { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" });
// Creates title for application
Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1\r\n\r\n");
Random rnd = new Random();
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("What champion would you like to select a skin for?.. ");
string champion = Console.ReadLine().ToLower();
Console.Write("Press the 'enter' key for a random champion.. ");
Console.ReadLine();
if(skins.ContainsKey(champion))
{
//return a random champion skin from the user input key in dict based on a random number from the length of the string array
Console.WriteLine(skins[champion][rnd.Next(skins[champion].Length)]);
}
}
添加到Dictionary 允许您通过检查冠军名称或Key 来简化流程,然后根据长度从string[] 数组或Value 中随机选择皮肤,这要归功于一个介于 0 和数组中元素计数之间的随机数。
希望这有助于萌芽:)
编辑:我有点无聊并玩弄了这个(无聊是指试图避免完成我的作业),所以我想出了一些对你更友好的东西。人们可能会讨厌我为您提供整个解决方案,但它向您展示了处理这些情况的方法,因此我将其视为以身作则。看看吧。
static void Main(string[] args)
{
Console.Clear(); //For a 'clean slate' on every execution, can ditch this if you want
Console.ForegroundColor = ConsoleColor.Gray;
Dictionary<string, string[]> skins = new Dictionary<string, string[]>();
skins.Add("ahri", new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
skins.Add("leesin", new string[] { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" });
Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1\r\n\r\n");
Console.WriteLine("What champion would you like to select a skin for? \r\nPress enter for a random champion... ");
var champion = Console.ReadLine();
var rnd = new Random();
if (champion.Equals(string.Empty))
{
var tmpList = Enumerable.ToList(skins.Keys);
champion = tmpList[rnd.Next(tmpList.Count)];
}
else
{
champion = champion.Trim().ToLower();
}
Console.Write(string.Format("Champion {0} Selected \r\n", champion));
if (skins.ContainsKey(champion))
{
Console.WriteLine(string.Format("Your random skin for {0} is: {1}\r\n", champion, skins[champion][rnd.Next(skins[champion].Length)]));
}
else
{
Console.Clear(); //clear the slate so console doesn't look cluttered.
Main(args);
}
}
现在除了你在英雄和他们的皮肤中添加了 127 个奇怪的东西之外,这已经基本完成了。
简明扼要,短小精悍。
- 对处理所有用户输入的
Console 进行一次调用,其余的都是自动化的
- 检查一个空的
string,如果它找到一个这是有效的并输入击键,就好像他们输入了一个不正确的值,它不会在Dict中找到,所以无论如何它都会“重新启动”程序。所以他们有两个选择:输入一个以string Key 表示的正确值,或者将其留空并按回车键。
- 如果找到空的
string(输入),它会将皮肤Dict 的Keys 转换为List,并根据元素的0 和Count() 之间的随机数随机选择一个内说Dict
- 否则,它将读取的输入转换为可用的字符串,然后继续
- 检查调整后的用户输入或随机选择的
string key 与Dict 的Keys 是否匹配,如果找到,则根据数组元素的Length 或Count 输出随机皮肤在那个KeysValue下,此时程序存在。
- 如果未找到
Key(仅基于错误的用户输入),则会清除控制台并“重新启动”应用程序。
这远不是可用的最佳实现,但它确实有效,我真的看不出它的逻辑有问题,如果它在某个地方有问题,可以随时更新我。假设我现在应该做我的作业。这 15 分钟用得很好,很有趣。一定喜欢编码:)
希望这会有所帮助,也许您现在可以研究一些东西并扩展您的知识。
稍后