question

PlaystationGamer-8084 avatar image
0 Votes"
PlaystationGamer-8084 asked PlaystationGamer-8084 commented

Choose multiple options within an array

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

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Consider using a class to store information for selections and if needed store the selections to a json file. For a menu, check out the following.


0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered PlaystationGamer-8084 commented

@PlaystationGamer-8084, 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.


image.png (21.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much, this has worked perfectly! I have accepted this as the correct solution.

0 Votes 0 ·