I have a multiple choice method, but sometimes i need it to be only 2 options, but sometimes 3, 4 or even 5. I want to make a dynamic method that takes in an int of how many options there should be, and that is how many options the user can choose from. So if i call the function with the int being 2, there will only be 2 choices, but i call it with 4, then there will be 4.
Here is the code of what i'm trying to achieve so you can see.
string OptionChoice(int howManyOptions)
{
bool optionChosenIsValid = false;
char optionChosen;
// If howManyOptions is 2, it should be like this ↓
do
{
Console.Write("\n\nMelyik opciót választod? (Nyomd meg az 'A' vagy 'B' billentyűt!) ");
optionChosen = Console.ReadKey().KeyChar;
switch (optionChosen)
{
case 'a':
case 'A':
optionChosenIsValid = true;
break;
case 'b':
case 'B':
optionChosenIsValid = true;
break;
default:
Console.Write("\nIlyen opció nincsen, kérlek próbáld újra!");
break;
}
} while (!optionChosenIsValid);
Console.WriteLine($"\n({optionChosen.ToString().ToUpper()}) lehetőség kiválasztva");
//But if howManyOptions is for example 4, it should be like this ↓
do
{
Console.Write("\n\nMelyik opciót választod? (Nyomd meg az 'A', 'B', 'C' vagy 'D' billentyűt!) ");
optionChosen = Console.ReadKey().KeyChar;
switch (optionChosen)
{
case 'a':
case 'A':
optionChosenIsValid = true;
break;
case 'b':
case 'B':
optionChosenIsValid = true;
break;
case 'c':
case 'C':
optionChosenIsValid = true;
break;
case 'd':
case 'D':
optionChosenIsValid = true;
break;
default:
Console.Write("\nIlyen opció nincsen, kérlek próbáld újra!");
break;
}
} while (!optionChosenIsValid);
Console.WriteLine($"\n({optionChosen.ToString().ToUpper()}) lehetőség kiválasztva");
return optionChosen.ToString().ToUpper();
}
I know i could just put the howManyOptions int in a switch case and whatever number it is, i'd have that many choices in it, but i feel like that is very ugly, and there must be a better solution. Thank you in advance!
Well At first you could try this