Conor February 2016

C# Using user input to select what string array to randomly print the elements from

I'm new to coding so please excuse my terminology..

I'm trying to make a random league of legends skin selector. I want the user to input the champion they want, in this case I have ahri and leeSin, then with the user input, I want it to select the string array and randomly print one of the elements. I think I'm pretty close but I cannot use a string with a string[]. Any help would be very appreciated.

namespace RandomLolChampSelector
{
    class Program
    {
        static void Main(string[] args)

        {
            string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
            string[] leeSin = { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" };

            // Creates title for application
            Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1");
            Console.WriteLine(" ");
            Console.WriteLine(" ");

            Random rnd = new Random();

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("What champion would you like to select a skin for?..    ");
            string champion = Console.ReadLine();

            foreach (string s in champion)
            {
                while (true)
                {
                    // Gets user to press a key to run the code
                    Console.Write("Press the 'enter' key for a random champion..     ");
                    string question = Console.ReadLine();

                    int randomNumber = rnd.Next(ahri.Length);
                    Console.WriteLine(ahri[randomNumber]);
                }
            }
        }
    }
}

Answers


Larry Smithmier February 2016

One approach is an array of arrays. Take a look at https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx, specifically where they talk about Array-of-arrays (jagged).

A (probably) more intuitive approach is a dictionary of arrays. Think about something like:

Dictionary<string, string[]> myList = new Dictionary<string, string[]>();
myList.Add("ahri",new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
//your other code
Console.WriteLine(myList[champion][randomNumber]);

Think about using the length of the array myList[champion].Length as the bounds for your random number to keep from getting an out of bounds error.


ArturoAP February 2016

Try this:

namespace RandomLolChampSelector
{
    class Program
    {
        static void Main(string[] args)

        {
            string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
            string[] leeSin = { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" };

            // Creates title for application
            Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1");
            Console.WriteLine(" ");
            Console.WriteLine(" ");

            Random rnd = new Random();

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("What champion would you like to select a skin for?..    ");
            string champion = Console.ReadLine();


                    // Gets user to press a key to run the code
             Console.Write("Press the 'enter' key for a random champion..     ");
             string question = Console.ReadLine();

             if(champion == "ahri")
             {
                int randomNumber = rnd.Next(ahri.Length-1);
                Console.WriteLine(ahri[randomNumber]);
             }
             else //If you have more than 2 arrays you will need another if or a case statement
             {
                int randomNumber = rnd.Next(leeSin.Length-1);
                Console.WriteLine(leeSin[randomNumber]);
             }
        }
    }
}


Dave February 2016

I have modified your original program (preserving as much of your code as possible) to produce the intended behavior. Please feel free to run as-is here and research what I have done to make it work:

class Program
{
    static void Main(string[] args)
    {
        string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
        string[] leeSin = { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" };

        // Creates title for application
        Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1");
        Console.WriteLine(" ");
        Console.WriteLine(" ");

        Random rnd = new Random();

        Console.ForegroundColor = ConsoleColor.Gray;

        // Stores what array has been selected:
        string[] champions;

        Console.WriteLine("What champion would you like to select a skin for?..    ");
        string championName = Console.ReadLine();
        if (championName.Equals("ahri", StringComparison.CurrentCultureIgnoreCase))
        {
            champions = ahri;
        }
        else if (championName.Equals("leeSin", StringComparison.CurrentCultureIgnoreCase))
        {
            champions = leeSin;
        }
        else 
        {
            // No champion selected, exit program:
            Console.WriteLine("No champion selected, quitting...");
            return;
        }

        while (true)
        {
            // Gets user to press a key to run the code
            Console.WriteLine("Press the 'enter' key for a random champion..     ");
            if (Console.ReadKey(true).Key == ConsoleKey.Enter)
            {                    
                int randomNumber = rnd.Next(champions.Length);
                Console.WriteLine(champions[randomNumber]);
            }
        }
    }
}


Gabe February 2016

Give this a shot:

    static void Main(string[] args)
        {
            Dictionary<string, string[]> skins = new Dictionary<string, string[]>();
            skins.Add("ahri", new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
            skins.Add("leesin", new string[] { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" });

            // Creates title for application
            Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1\r\n\r\n");

            Random rnd = new Random();

            Console.ForegroundColor = ConsoleColor.Gray;

            Console.WriteLine("What champion would you like to select a skin for?..    ");
            string champion = Console.ReadLine().ToLower();

            Console.Write("Press the 'enter' key for a random champion..     ");
            Console.ReadLine();
            if(skins.ContainsKey(champion))
            {
                //return a random champion skin from the user input key in dict based on a random number from the length of the string array
                Console.WriteLine(skins[champion][rnd.Next(skins[champion].Length)]);

            }

        }

Adding to a Dictionary allows you to simplify the process by being able to check against the champion name or Key and then randomly select a skin from the string[] array or Value based on its length thanks to a random number between 0 and the count of the elements within the array.

Hope this helps bud :)


Edit: I got bored and played around with this a bit (by bored I mean trying to avoid going my assignments) so I came up with something a little more user friendly for you. People will probably hate me for giving you the entire solution but it shows you ways to handle these situations so I see it as teaching by example. Have a look.


              

Post Status

Asked in February 2016
Viewed 1,766 times
Voted 5
Answered 4 times

Search




Leave an answer