I am making an interface to store character and dinosaur variables to a list and then print them out when asked.
There is a switch statement to take the users' inputs:
- Case 1: The function allows the user to input character data and then it's put into a list.
- Case 2.: The function allows the user to input dinosaur data and then it put into a list.
- Case 3 The function prints the objects in each list. However, whenever I run it it doesn't print anything and comes up with no errors. If I run the code to print each list in their respective case then they print out fine.
class Program
{
static void Main()
{
int NumAns;
Console.WriteLine("*1. Character Creator ");
Console.WriteLine("*2. Dinosaur Creator ");
Console.WriteLine("*3. Display ");
Console.WriteLine("");
Console.WriteLine("Specify your menu choice");
string NumAnsString = Console.ReadLine();
NumAns = Convert.ToInt32(NumAnsString);
MenuChosen(NumAns);
}
static void MenuChosen(int selection)
{
List<Dinosaur> Dinosaur_l = new List<Dinosaur>();
List<Character> CharSave = new List<Character>();
Character character = new Character();
Dinosaur dinosaur = new Dinosaur();
switch (selection)
{
case 1:
Console.WriteLine("You chose Character Creator");
character.CharacterVal();
CharSave.Add(new Character
{
name = character.name,
age = character.age,
height = character.height,
hair = character.hair,
eyes = character.eyes,
glasses = character.glasses
});
Main();
break;
case 2:
Console.WriteLine("You chose Dinosaurs");
dinosaur.DinoVal();
Dinosaur_l.Add(new Dinosaur
{
name = dinosaur.name,
species = dinosaur.species,
diet = dinosaur.diet,
sex = dinosaur.sex
});
Main();
break;
case 3:
Console.WriteLine(" ");
Console.WriteLine("Character: ");
Console.WriteLine(" ");
foreach (Character chara in CharSave)
{
Console.WriteLine(chara.name);
Console.WriteLine(chara.age);
Console.WriteLine(chara.height);
Console.WriteLine(chara.hair);
Console.WriteLine(chara.eyes);
Console.WriteLine(chara.glasses);
}
Console.WriteLine(" ");
Console.WriteLine("Dinosaurs: ");
Console.WriteLine(" ");
foreach (Dinosaur Dino in Dinosaur_l)
{
Console.WriteLine(Dino.name);
Console.WriteLine(Dino.species);
Console.WriteLine(Dino.diet);
Console.WriteLine(Dino.sex);
}
Console.WriteLine(" ");
Main();
break;
}
}
}
This is the code used for CharacterVal function
public class Character
{
public string name;
public string age;
public string height;
public string hair;
public string eyes;
public string glasses;
public int charID = 0;
int x = 1;
public void CharacterVal()
{
charID ;
Console.WriteLine("Name your Character: ");
name = Console.ReadLine();
Console.WriteLine("How old is " name "? :");
age = Console.ReadLine();
Console.WriteLine("How tall is " name "?: ");
height = Console.ReadLine();
Console.WriteLine("What colour is " name "'s hair ?: ");
hair = Console.ReadLine();
Console.WriteLine("What colour is " name "'s eyes ?: ");
eyes = Console.ReadLine();
GlassesCheck();
}
public void GlassesCheck()
{
Console.WriteLine("does " name " wear glasses? (yes/no): ");
while (x == 1)
{
glasses = Console.ReadLine();
if(glasses =="yes")
{
x = 2;
}
else if(glasses == "no")
{
x = 2;
}
else
{
x = 1;
Console.WriteLine("'" glasses "' is not a valid answer, does " name " wear glasses? (yes/no): ");
}
}
}
}
This is the code used for the DinoVal function
public class Dinosaur
{
public string species;
public string diet;
public string sex;
public string name;
public void DinoVal()
{
Console.WriteLine("***************************");
Console.WriteLine("*Choose a Dinosaur *");
Console.WriteLine("* *");
Console.WriteLine("*1. Alvarezsaurus *");
Console.WriteLine("*2. Avimimus *");
Console.WriteLine("*3. Bactrosaurus *");
Console.WriteLine("*4. Baryonyx *");
Console.WriteLine("*5. Coloradisaurus *");
Console.WriteLine("*6. Chungkingosaurus *");
Console.WriteLine("***************************");
int Selection = Convert.ToInt32(Console.ReadLine());
switch (Selection)
{
case 1:
Console.WriteLine("you have chosen the Alvarezsaurus");
Console.WriteLine("This is a Carnivore ");
species = "Alvarezsaurus";
diet = "Carnivore";
break;
case 2:
Console.WriteLine("you have chosen the Avimimus");
Console.WriteLine("This is an Omnivore ");
species = "Avimimus";
diet = "Omnivore";
break;
case 3:
Console.WriteLine("you have chosen the Bactrosaurus");
Console.WriteLine("This is a Herbivore ");
species = "Bactrosaurus";
diet = "Herbivore";
break;
case 4:
Console.WriteLine("you have chosen the Baryonyx");
Console.WriteLine("This is a Carnivore ");
species = "Baryonyx";
diet = "Carnivore";
break;
case 5:
Console.WriteLine("you have chosen the Coloradisaurus");
Console.WriteLine("This is an Omnivore ");
species = "Coloradisaurus";
diet = "Omnivore";
break;
case 6:
Console.WriteLine("you have chosen the Chungkingosaurus");
Console.WriteLine("This is a Herbivore ");
species = "Chungkingosaurus";
diet = "Herbivore";
break;
}
Console.WriteLine("Please choose the dinosaurs sex");
Console.WriteLine("1.Male");
Console.WriteLine("2.Female");
int GenderChoice = Convert.ToInt32(Console.ReadLine());
switch (GenderChoice)
{
case 1:
Console.WriteLine("the dinosaur will be male");
sex = "Male";
break;
case 2:
Console.WriteLine("the dinosaur will be female");
sex = "Female";
break;
}
Console.WriteLine("What name will you give this " sex " " species " ?:");
name = Console.ReadLine();
}
}
i've tried moving
CharSave.Add(new Character
{
name = character.name,
age = character.age,
height = character.height,
hair = character.hair,
eyes = character.eyes,
glasses = character.glasses
});
into case 3 but all that does is print blank spaces for each object
CodePudding user response:
The lists that store the characters and the dinos need to be outside of the MenuChosen
method because as you code is written everytime MenuChosen
is called new lists are created.
You could:
class Program
{
static List<Dinosaur> Dinosaur_l = new List<Dinosaur>();
static List<Character> CharSave = new List<Character>();
}
Main -> MenuChosen -> Main -> MenuChosen -> ...
Because the way you structured (Main calls MenuChosen which calls Main and so on) your code - if you run the game for long enough - will get end up with a stack overflow.
To avoid this you can run your code in a loop:
Main()
{
while (true) // or selection was 'Quit', etc.
{
your code here
}
// Remove calls to 'Main' from the switch statement.