Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建包含数字数组和字符串数组的随机类_C#_Arrays_String_Random - Fatal编程技术网

C# 如何创建包含数字数组和字符串数组的随机类

C# 如何创建包含数字数组和字符串数组的随机类,c#,arrays,string,random,C#,Arrays,String,Random,我正在做一个GUI按钮。当用户点击它时,它会随机得到一个数字或一个单词。我知道如何处理数字,但我不知道如何处理文字和数字 int[] numbers = new int[5] { 100, 500, 1000, 5000, 20000}; Random rd = new Random(); int randomIndex = rd.Next(0, 5); int randomNumber = numbers[randomIndex]; button1.Text = randomNumber.To

我正在做一个GUI按钮。当用户点击它时,它会随机得到一个数字或一个单词。我知道如何处理数字,但我不知道如何处理文字和数字

int[] numbers = new int[5] { 100, 500, 1000, 5000, 20000};
Random rd = new Random();
int randomIndex = rd.Next(0, 5);
int randomNumber = numbers[randomIndex];
button1.Text = randomNumber.ToString(); 

字符串
的一个解决方案是创建一个要显示的
字符串
列表
,然后通过
random.Next()
获取随机数,以显示该特定
索引中的
字符串
。比如:

List<string> words = new List<string> { "Dog", "Cat", "Bird", "Monkey" };
Random rnd = new Random();

... and then in your implementation of the Button Click

int index = rnd.Next(words.Count); //important to limit the random result by the number of the words available
string randomString = words[index]; //Here it is
button1.Text = randomString; 
List words=新列表{“狗”、“猫”、“鸟”、“猴”};
随机rnd=新随机();
... 然后在你的实现中点击按钮
int index=rnd.Next(words.Count)//通过可用字数限制随机结果很重要
字符串随机字符串=单词[索引]//给你
button1.Text=随机字符串;

您有到目前为止已经完成的代码吗?似乎您需要
列表
来获取随机
字符串
@Donald yup,试试看!)既然你要把它作为文本放在一个按钮中,你可以把数字作为字符串放在列表中:
newlist{“Dog”、“Cat”、“Bird”、“Monkey”、“100”、“500”、“1000”}@diynevala是的,没错。因为
文本
最终是一个
字符串
;)@Ian使用数字作为字符串甚至有一个很好的理由:如果你碰巧把一个以零开头的数字(例如邮政编码00100)作为一个整数,它将显示在前面而不带零(例如“100”)。