/ / C#:文字列をランダムに使用する方法[終了]-c#、ランダム

C#:どのようにランダムに文字列を使用するのですか? [終了] - c#、ランダム

C#プログラミングで問題が発生しているので、ユーザーが名前を入力して次の操作を実行できるプログラムを作成します。

  1. 1つのボタンをクリックすると、これらの名前の1つの名前がランダムに表示されます

  2. 他のボタンはすべての名前を表示する必要がありますが、2つの名前の間にスペースを空けてください(グループ化など)。

ありがとうございました

回答:

回答№1は0

「名前」と呼ばれる配列に名前を収集したと仮定して、次の手順を実行してランダムに1つを選択します。

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.

また、すべての名前を1つの文字列にまとめて名前を間にするには、次を使用します。

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.
}
}

必要な中間ステップがある場合は、: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);
}