C# 使用用户输入选择要从中随机打印元素的字符串数组
本文关键字:打印 随机 元素 数组 字符串 用户 输入 选择 | 更新日期: 2023-09-27 18:33:41
我是编码新手,所以请原谅我的术语。
我正在尝试制作一个随机的英雄联盟皮肤选择器。我希望用户输入他们想要的冠军,在这种情况下,我有ahri和leeSin,然后通过用户输入,我希望它选择字符串数组并随机打印其中一个元素。我想我很接近,但我不能使用带有字符串的字符串[]。任何帮助将不胜感激。
namespace RandomLolChampSelector
{
class Program
{
static void Main(string[] args)
{
string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
string[] leeSin = { "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");
Console.WriteLine(" ");
Console.WriteLine(" ");
Random rnd = new Random();
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("What champion would you like to select a skin for?.. ");
string champion = Console.ReadLine();
foreach (string s in champion)
{
while (true)
{
// Gets user to press a key to run the code
Console.Write("Press the 'enter' key for a random champion.. ");
string question = Console.ReadLine();
int randomNumber = rnd.Next(ahri.Length);
Console.WriteLine(ahri[randomNumber]);
}
}
}
}
}
一种方法是数组数组。 看看 https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71(.aspx,特别是他们谈论数组数组(交错(的地方。
(可能(更直观的方法是数组字典。 想一想:
Dictionary<string, string[]> myList = new Dictionary<string, string[]>();
myList.Add("ahri",new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
//your other code
Console.WriteLine(myList[champion][randomNumber]);
考虑使用数组myList[champion].Length
的长度作为随机数的边界,以防止出现越界错误。
试一试:
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
的正确值,或者将其留空并按 Enter 键。 - 如果找到(输入(空
string
,它将皮肤Dict
Keys
转换为List
,并根据 0 和所述Count()
元素之间的随机数随机选择一个Dict
- 否则,它将读取输入转换为可用的字符串,以达到此处的目的并继续前进
- 检查调整后的用户输入或随机选择的
string
key
是否与Dict
的Keys
(如果找到(,根据该Keys
Value
下数组元素的Length
或Count
输出随机皮肤,此时程序存在。 - 如果未找到
Key
(仅基于不正确的用户输入(,则会清除控制台并"重新启动"应用程序。
这远非可用的最佳实现,但它有效,我真的看不出它的逻辑有问题,如果它在某处有问题,可以随时更新我。假设我现在应该做我的作业。这是15分钟,很有趣。必须喜欢编码:)
希望这有所帮助,也许您现在有一些可以研究并扩展您的知识的东西。
后
试试这个:
namespace RandomLolChampSelector
{
class Program
{
static void Main(string[] args)
{
string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
string[] leeSin = { "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");
Console.WriteLine(" ");
Console.WriteLine(" ");
Random rnd = new Random();
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("What champion would you like to select a skin for?.. ");
string champion = Console.ReadLine();
// Gets user to press a key to run the code
Console.Write("Press the 'enter' key for a random champion.. ");
string question = Console.ReadLine();
if(champion == "ahri")
{
int randomNumber = rnd.Next(ahri.Length-1);
Console.WriteLine(ahri[randomNumber]);
}
else //If you have more than 2 arrays you will need another if or a case statement
{
int randomNumber = rnd.Next(leeSin.Length-1);
Console.WriteLine(leeSin[randomNumber]);
}
}
}
}
我已经修改了您的原始程序(保留尽可能多的代码(以产生预期的行为。 请随意在这里按原样运行并研究我为使其工作所做的工作:
class Program
{
static void Main(string[] args)
{
string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
string[] leeSin = { "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");
Console.WriteLine(" ");
Console.WriteLine(" ");
Random rnd = new Random();
Console.ForegroundColor = ConsoleColor.Gray;
// Stores what array has been selected:
string[] champions;
Console.WriteLine("What champion would you like to select a skin for?.. ");
string championName = Console.ReadLine();
if (championName.Equals("ahri", StringComparison.CurrentCultureIgnoreCase))
{
champions = ahri;
}
else if (championName.Equals("leeSin", StringComparison.CurrentCultureIgnoreCase))
{
champions = leeSin;
}
else
{
// No champion selected, exit program:
Console.WriteLine("No champion selected, quitting...");
return;
}
while (true)
{
// Gets user to press a key to run the code
Console.WriteLine("Press the 'enter' key for a random champion.. ");
if (Console.ReadKey(true).Key == ConsoleKey.Enter)
{
int randomNumber = rnd.Next(champions.Length);
Console.WriteLine(champions[randomNumber]);
}
}
}
}