Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJordan Jessee
664 PointsSo I'm making a text base game in C# and I am getting stuck on a switch statement that is creating players.
Heres the class
using System; public class PlayerBase{ public string playerType; public string playerName;
public void typeSelection(){
Console.WriteLine("What type of creature are you?");
Console.WriteLine("Dragon, Werewolf, Yeti, Imp, Human");
playerType = Console.ReadLine();
public int health;
public int armor;
public int power;
switch(playerType) {
case "Dragon":
Console.WriteLine("Well hello mighty creature!");
PlayerBase.health = 100;
PlayerBase.armor = 200;
PlayerBase.power = 100;
public string Magic.name = "fire";
break;
case "Werewolf":
Console.WriteLine("Oh my, Look at those teeth!");
PlayerBase.health = 100;
PlayerBase.armor = 75;
PlayerBase.power = 100;
public string Magic.name = "Bite";
break;
case "Yeti":
Console.WriteLine("So you do exist!");
PlayerBase.health = 100;
PlayerBase.armor = 75;
PlayerBase.power = 100;
public string Magic.name = "Freeze";
break;
case "Imp":
Console.WriteLine("Why you little devil, Welcome!"):
PlayerBase.health = 100;
PlayerBase.armor = 25;
PlayerBase.power = 50;
public string Magic.name = "Multiply";
break;
case "Human":
Console.WriteLine("A human? HaHa, Good Luck!");
PlayerBase.health = 100;
PlayerBase.armor = 15;
PlayerBase.power = 25;
break;
default:
Console.WriteLine("This is not an acceptable race!!");
break;
}
}
2 Answers
Steven Parker
231,210 PointsYou forgot to include a context to test the code in.
For a large project, it might be best to provide a link to a workspace snapshot or github repo.
You also didn't say exactly what kind of issue you are having, but at first glance I noticed a few things. First, you assign a variable named playerType without declaring it. Doesn't that produce a compiler error?
I also noticed that some case
blocks include a statement like: public string Magic.name = "(something)"
— but you probably don't want to declare a variable inside the switch statement since it won't be accessible later.
And you don't need to prefix your instance variables with the class name.
If those hints don't resolve your issue, try describing your issue in more detail and posting enough code to test the problem.
Receipt Box
1,866 PointsAgreed with what Steven wrote. If you haven't already, I'd recommend setting up Visual Studio, as it will automatically highlight syntax errors in your code which are preventing it from compiling. I would not recommend writing a relatively large program in Treehouse's workspace, as it is relatively primitive when it comes to finding bugs.
https://teamtreehouse.com/library/introduction-to-visual-studio
https://teamtreehouse.com/library/using-github-with-visual-studio
EDIT: Although looking at your github repo, it looks like you've already done that and fixed the error. Yay!
Jordan Jessee
664 PointsJordan Jessee
664 PointsThe github repo is https://github.com/jomaje8/CSharpFinal