I am brand new to coding and I am re-creating the start of a Pokemon game in order to practice. I am using the 'while' with cases and I currently have this (please note I have created a method for TypeLine to print the text but a character at a time like dialogue rather than the full sentence all at once. This code is working so ignore that and imagine it as Console.WriteLine);
//Player makes their Pokemon choice.
bool pokemonChoice = true;
while (pokemonChoice)
{
string input = Console.ReadLine();
int key = Convert.ToInt32(input);
switch (key)
{
case 1:
TypeLine("\nCharmander! What an excellent decision! \n");
TypeLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
pokemonChoice = false;
break;
case 2:
TypeLine("\nSquirtle! What an excellent decision! \n");
TypeLine("CONGRATULATIONS! YOU JUST OBTAINED SQUIRTLE! \n");
pokemonChoice = false;
break;
case 3:
TypeLine("\nBulbasaur! What an excellent decision! \n");
TypeLine("CONGRATULATIONS! YOU JUST OBTAINED BULBASAUR! \n");
pokemonChoice = false;
break;
What I want to do, is if say case 1 is selected by the user inputting "1", then a new string is created called "pokemon" and is assigned "Charmander".
string pokemon = ("Charmander");
The purpose is so that later, instead of having to type the Pokemon name, I could use;
Console.WriteLine(pokemon + " I choose you!");
I attempted to do so in the case itself, like this;
switch (key)
{
case 1:
string pokemon = ("Charmander");
TypeLine("\nCharmander! What an excellent decision! \n");
TypeLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
pokemonChoice = false;
break;
But when I then try to use it outside of the 'while', I get the error "The name 'pokemon' does not exist in the current context"
How can I achieve the result I have detailed?
Thanks in advance!