dimanche 3 septembre 2017

Choosing random index from an array which will link to the same index of another array C#

I acknowledge that the title seems very confusing, I was just unable to word it correctly, rest assured I will explain my problem in depth here.

I will first explain what I am wanting to accomplish as a whole.

I am wanting to create a program that will ask the user to guess the country name based on a displayed capital city. The user will now have the choice to enter up to three country names, in one line, each separated by a comma, displaying a warning if the user enters more than the three that are asked for. The program will then generate a random number between (0-5). The random number will be used as an index to retrieve both a capital city from the capital city array and the corresponding country name (which would be the correct answer).

I started trying to solve it, but I am just not sure what my best choice should be to generate a random number between 0-5 that links to the city and then link them to the corresponding country.

Any help would be appreciated If you need more information just ask. I will post the code of what I have attempted below:

        string[] capitals = new string[] { "Athens", "Bangkok", "Beijing", "Berlin", "Amsterdam", "Ankara" };
        string[] countrynames = new string[] { "Greece", "Thailand", "China", "Germany", "Netherlands", "Turkey" };

        string random = capitals[new Random().Next(0, 5)];

        WriteLine("Which country has the capital city {0}? ", random);
        Write("Enter up to 3 names, comma-seperated: ");
        string userinput = Console.ReadLine();
        string[] temp = userinput.Split(',');




Aucun commentaire:

Enregistrer un commentaire