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


i'm new coding please excuse terminology..

i'm trying make random league of legends skin selector. want user input champion want, in case have ahri , leesin, user input, want select string array , randomly print 1 of elements. think i'm pretty close cannot use string string[]. 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 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 select skin for?..    ");             string champion = console.readline();              foreach (string s in champion)             {                 while (true)                 {                     // gets user press key run code                     console.write("press 'enter' key random champion..     ");                     string question = console.readline();                      int randomnumber = rnd.next(ahri.length);                     console.writeline(ahri[randomnumber]);                 }             }         }     } } 

give 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 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 select skin for?..    ");             string champion = console.readline().tolower();              console.write("press 'enter' key random champion..     ");             console.readline();             if(skins.containskey(champion))             {                 //return random champion skin user input key in dict based on random number length of string array                 console.writeline(skins[champion][rnd.next(skins[champion].length)]);              }          } 

adding dictionary allows simplify process being able check against champion name or key , randomly select skin string[] array or value based on length random number between 0 , count of elements within array.

hope helps bud :)


edit: got bored , played around bit (by bored mean trying avoid going assignments) came little more user friendly you. people hate me giving entire solution shows ways handle these situations see teaching example. have look.

static void main(string[] args) {     console.clear(); //for 'clean slate' on every execution, can ditch if want     console.foregroundcolor = consolecolor.gray;      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" });      console.writeline("conor's random league of legends skin selector v0.1\r\n\r\n");     console.writeline("what champion select skin for? \r\npress enter random champion...    ");      var champion = console.readline();      var rnd = new random();     if (champion.equals(string.empty))     {         var tmplist = enumerable.tolist(skins.keys);         champion = tmplist[rnd.next(tmplist.count)];     }     else     {         champion = champion.trim().tolower();      }      console.write(string.format("champion {0} selected \r\n", champion));      if (skins.containskey(champion))      {         console.writeline(string.format("your random skin {0} is: {1}\r\n", champion, skins[champion][rnd.next(skins[champion].length)]));     }     else     {         console.clear(); //clear slate console doesn't cluttered.         main(args);      } } 

now pretty complete except 127 odd additions in champions , skins.

quick rundown, short , sweet.

  • does single call console handles user input, rest automated
  • checks empty string if finds 1 , enter keystroke if enter incorrect value won't found in dict 'restart' program anyway. they're left 2 options: enter correct value represented string key or leave blank , hit enter.
  • if , empty string found (enter) converts keys of skins dict list , randomly selects 1 based on random number between 0 , count() of elements within said dict
  • else converts read input useable string purpose here , moves on
  • checks if adjusted user input or randomly selected string key against keys of dict if found, outputs random skin based on length or count of array elements under keys value, @ time program exists.
  • if key isn't found (only based on incorrect user input) clears console , 'restarts' application.

this far best possible implementation available works , can't see problem it's logic, can feel free update me if has issue somewhere. suppose should assignments now. 15 mins spent, entertaining. gotta love coding :)

hope helps , maybe have few things can research , expand knowledge on.

later


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -