vendredi 2 avril 2021

A faster way than typing multiple else if statements [closed]

I'm doing a random name generator for enemies in my console game, I have it pick a random number, and based on the number it gives the enemy a name. I want a cleaner look with my code and also for future reference don't want to type out 10+ else if statements. Is there an easier way to do this, or am I just going to have to continue doing it like this?

void chooseName()
{
    Random numGen = new Random();
    int number = numGen.Next(0, 10);
    
    if (number == 1)
    { }
 
    else if (number == 2)
    { }

    else if (number == 3)
    { }

    else if (number == 4)
    { }

    else if (number == 5)
    { }

    else if (number == 6)
    { }

    else if (number == 7)
    { }

    else if (number == 8)
    { }

    else if (number == 9)
    { }

    else
    { }
}

Aucun commentaire:

Enregistrer un commentaire