Sto riscontrando un problema con la programmazione in C #, quindi desidero creare un programma che consenta all'utente di inserire i nomi e quindi effettuare le seguenti operazioni:
facendo clic su un pulsante dovrebbe mostrare un nome di questi nomi in modo casuale
e l'altro pulsante dovrebbe visualizzare tutti i nomi ma creare spazi tra i 2 nomi (come raggruppamento o qualcosa del genere).
Grazie
risposte:
0 per risposta № 1Supponendo che tu abbia raccolto i nomi in un array chiamato "nomi", procedi come segue per sceglierne uno a caso.
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.
E per riunire tutti i nomi in una stringa con un nome in mezzo, utilizzare quanto segue:
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.
}
}
Se sono necessari passaggi intermedi, basta chiedere: p
Ecco una versione modificata del tuo codice.
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);
}