Okay, a few things:
Your UserClass
is an array
. It will hold items, but unless you are appending to it, you cannot write user input to it. Here is a reference for Arrays
in C# from MSDN.
You are using .Contains()
on a string array
instead of a string
. While logically, yes, the string array
does contain those values. Although, if it actually ran/compiled, all the if statements
would run true rather than choosing from user input.
Which leads me to my next thing- you ask for user input
but never actually allow for it within the as-shown Main()
method. The most common way (that I've seen) is something like:
string input = Console.ReadLine();
Which, I see you are trying to implement, so that isn't an issue :)
This code should work for (and I'll break down my changes):
static void Main(string[] args)
{
string userName = "James";
string[] userClass = new string[3] { "mage", "warrior", "assassin" };
Console.WriteLine("What class will you be? You can choose from Mage, Warrior or Assassin:");
string input = Console.ReadLine();
if (input.ToLower() == userClass[0])
{
string Message = "You are a strong Willed Mage " + userName;
Console.WriteLine(Message);
}
else if (input.ToLower() == userClass[1])
{
string Message = "Your are a valiant Warrior " + userName;
Console.WriteLine(Message);
}
else if (input.ToLower() == userClass[2])
{
string Message = "You are a vigilant Assassin " + userName;
Console.WriteLine(Message);
}
else
Console.WriteLine("No proper class selected...");
Console.ReadLine();
}
string userName
remains the same along with your string[] userClass
(I have altered the capitalization to camel-casing). There is no issue (that I see) in having these userClass
s stored within an array
, as long as you are checking against the array
properly.
Instead of checking if the string[] userClass
array contains these items, because we know it does as we've written it and as stated before, it would always run true. Instead, we check to see if the user input
matches something within the array.
I have created string input = Console.ReadLine()
to allow for user input, and then I check input
against the string[] userClass
values. I have added .ToLower()
and changed the array values to all lower case. This way, if a user inputs mAgE
instead of Mage
, there will not be an error thrown.
Another way to go about it, if you are wanting to avoid using an array
, is to use a switch:
switch (input.ToLower())
{
case "mage":
Console.WriteLine($"You are a strong Willed Mage {userName}");
//assign user class
break;
case "warrior":
Console.WriteLine($"Your are a valiant Warrior {userName}");
//assign user class
break;
case "assassin":
Console.WriteLine($"You are a vigilant Assasin {userName}");
//assign user class
break;
}