Choose multiple options within an array

Playstation Gamer 41 Reputation points
2022-05-03T19:15:40.25+00:00

Hi. I'm struggling to figure out how multiple options can be chosen from an array in C#. I have provided some code of the array that I have created (see below).

string[] AddionalExtras = new[] { "1 - 16” Alloys", "2 - Privacy Glass", "3 - Spare Wheel", "4 - Parking Pack", "5 - SYNC 3 with Navigation", "6 - Winter Pack", "7 - Floor mats", "8 - Boot liner", "9 - Dashboard camera", "10 - Pet travel mat" }; // Any additional extras will be displayed in the array
Console.WriteLine(String.Join(Environment.NewLine, AddionalExtras)); // All of the extras are displayed from the array, line by line
Console.WriteLine("Please select any of the extras that you would like to purchase!");
int ExtraSelection = Convert.ToInt32(Console.ReadLine()); // Integer called 'ExtraSelection' created and will be set based on the users input

What I would like is for when at this section in my console application, the user should be allowed to pick more than one item, if they wish to, and then have that selection output back to them (I should have no problem coding that part). Is there an efficient and easy way to do this?

Within my application, most, if not all of my other arrays are also strings, but only require one option, so this is new to me. I am still relatively new to the language, so apologies if there are glaring errors, or an obvious way around this (I did try searching for a solution, but couldn't find anything).

I hope this is enough code to help answer my question.

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-05-04T02:11:38.64+00:00

    @Playstation Gamer , Welcome to Microsoft Q&A, I would recommend that you use dictionary to store the menu options and use list to save the choose options.

    Here is a code example you could refer to.

       static void Main(string[] args)  
            {  
                string[] AddionalExtras = new[] { "1 -  Alloys", "2 - Privacy Glass", "3 - Spare Wheel", "4 - Parking Pack", "5 - SYNC 3 with Navigation", "6 - Winter Pack", "7 - Floor mats", "8 - Boot liner", "9 - Dashboard camera", "10 - Pet travel mat" }; // Any additional extras will be displayed in the array  
                Console.WriteLine(String.Join(Environment.NewLine, AddionalExtras)); // All of the extras are displayed from the array, line by line  
                List<string> list = new List<string>();  
                Dictionary<int,string> dic=new Dictionary<int, string>();  
                foreach (string item in AddionalExtras)  
                {  
                    int n =Convert.ToInt32(item.Split('-')[0].Trim());  
                    string m = item.Split('-')[1].Trim();  
                    dic.Add(n, m);  
                }  
                while (true)  
                {  
                    Console.WriteLine("Please select any of the extras that you would like to purchase!");  
                     
                    int ExtraSelection = Convert.ToInt32(Console.ReadLine());  
              
                    if (dic.ContainsKey(ExtraSelection))  
                    {  
                        list.Add(dic[ExtraSelection]);  
                    }  
                    else  
                    {  
                        Console.WriteLine("Please input the correct number");  
                    }  
                    ConsoleKeyInfo c = Console.ReadKey();  
                    if (c.Key == ConsoleKey.Spacebar)  
                    {  
                        break;  
                    }  
      
                }  
                Console.WriteLine("The next is your purchase list");  
                foreach (var item in list)  
                {  
                    Console.WriteLine(item);  
                }  
                Console.ReadKey();  
            }  
    

    Note: If you want to end the choose, Please click O in the console otherwise click enter to continue to purchase something.

    Result:

    198628-image.png


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful