Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(SOLVED) How do i make a dynamic multiple choice method in C#?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Well At first you could try this

  1. Generate the console logs dynamically based on the number of "howManyOptions" you pass to the function.
  2. Use the dynamic loop again to check if the chosen option is within the acceptable range or not.
string OptionChoice(int howManyOptions)
{
    bool optionChosenIsValid = false;
    char optionChosen;


    do
{
    Console.Write($"\n\nMelyik opciót választod? (Nyomd meg az ");
    
    // Construct your options list with this loop
    for (int i = 0; i < howManyOptions; i++)
    {
        char optionKey = (char)('A' + i);
        Console.Write($"'{optionKey}'{(i < howManyOptions - 1 ? ", " : "")}");
    }

    Console.Write(" billentyűt!) ");
    
    optionChosen = Console.ReadKey().KeyChar;
    
    // similarly check if the chosen operation is within the option range you expect it to be
    for (int i = 0; i < howManyOptions; i++)
    {
        char optionKey = (char)('A' + i);
        if (char.ToUpper(optionChosen) == optionKey)
        {
            optionChosenIsValid = true;
            break;
        }
    }

    if (!optionChosenIsValid)
    {
        Console.Write("\nIlyen opció nincsen, kérlek próbáld újra!");
    }

} while (!optionChosenIsValid);

Console.WriteLine($"\n({optionChosen.ToString().ToUpper()}) lehetőség kiválasztva");
    return optionChosen.ToString().ToUpper();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading