C#:如何随机使用字符串?

C#:如何随机使用字符串?,c#,random,C#,Random,我在C#编程方面遇到问题,因此我想制作一个程序,让用户输入名称,然后执行以下操作: string output = "";//Initialise a variable for all the names to exist inside. for(int i = 0; i < names.Length; i++)//For every value in "names" { output += names[i];//"output" has the newest value of "

我在C#编程方面遇到问题,因此我想制作一个程序,让用户输入名称,然后执行以下操作:

string output = "";//Initialise a variable for all the names to exist inside.
for(int i = 0; i < names.Length; i++)//For every value in "names"
{
    output += names[i];//"output" has the newest value of "names" added onto the end.
    if (i < names.Length - 1)//If the name isn't the last one.
    {
        output+= " ";//Add the space.
    }
}
  • 通过单击一个按钮,它将随机显示这些名称中的一个名称

  • 另一个按钮应该显示所有的名称,但在每两个名称之间留有空格(比如分组之类的)


  • 谢谢

    假设您已将名称收集到一个名为“名称”的数组中,请执行以下操作以随机选择一个

    Random rand = new Random();//Make a new instance of the "Random" class.
    string randomname = names[rand.Next(names.Length)];//Sets "randomname" to the name value with a given index, this index is a random number between 0 and the length of the array.
    
    要将所有名称收集到一个名称介于两者之间的字符串中,请使用以下命令:

    string output = "";//Initialise a variable for all the names to exist inside.
    for(int i = 0; i < names.Length; i++)//For every value in "names"
    {
        output += names[i];//"output" has the newest value of "names" added onto the end.
        if (i < names.Length - 1)//If the name isn't the last one.
        {
            output+= " ";//Add the space.
        }
    }
    
    字符串输出=”//初始化一个变量,使所有名称都存在其中。
    for(int i=0;i
    如果你需要任何中间步骤,只要问:p

    这是您的代码的修改版本

     string[] myNames = new string[25]; //declare it globally 
     int index = 0; //declare it globally 
     string name = ""; // globally 
     private void findButton_Click(object sender, EventArgs e) 
     { 
         Random rand = new Random();//Make a new instance of the "Random" class. 
         string randomname = myNames[rand.Next(myNames.Length)]; 
     } 
     private void listGroupButton_Click(object sender, EventArgs e) 
     {
            //list group 
     }
     private void listGroupButton_Click(object sender, EventArgs e) 
     { //list group 
         string output = "";//Initialise a variable for all the names to exist inside. 
         for(int i = 0; i < myNames.Length; i++)//For every value in "names" 
         { 
             output += myNames[i];//"output" has the newest value of "names" added onto the end. 
             if (i < myNames.Length - 1)//If the name isn't the last one. 
             {
                 output+= " ";//Add the space. 
             } 
         } 
         MessageBox.Show(output);
     } 
    

    string[]myNames=新字符串[25]//全球申报
    int指数=0//全球申报
    字符串名称=”;//全球地
    私有void findButton_单击(对象发送方,事件参数e)
    { 
    Random rand=new Random();//创建“Random”类的新实例。
    string randomname=myNames[rand.Next(myNames.Length)];
    } 
    
    private void listGroupButton_单击(对象发送者,事件参数e) { //列表组 }
    private void listGroupButton_单击(对象发送者,事件参数e) {//列表组 string output=“;//初始化一个变量,使其中存在所有名称。 for(int i=0;i
    您是否一直坚持输入姓名?处理按钮点击?随机挑选?显示结果?你有没有一些你刚开始使用的代码不起作用?谢谢,但是我应该用什么来在信息中显示结果呢?输出?MessageBox.Show(“+”输出);因为它不工作='(它给出了什么错误?如果你在问题中发布了你的代码,那会有帮助。string[]myNames=new string[25];//全局声明它int index=0;//全局声明它string name=“”;//全局私有void findButton_单击(对象发送方,事件参数e){//random rand=new random();//创建“random”类的新实例。string randomname=names[rand.Next(names.Length)];}private void listGroupButton_单击(object sender,EventArgs e){//list group string output=“”;//初始化一个变量,使所有名称都存在于其中。例如(int i=0;i