Reputation: 55
i am trying to learn, (im a newb to coding)
i wrote this
static void Main(string[] args)
{
string username = "James";
string[] Userclass = new string[3] { "Mage", "Warrior", "Assasin" };
Console.WriteLine("What class will you be? You can choose from Mage, Warrior, Or Assasin: ");
if (Userclass.Contains("Mage"))
{
String Message = "You are a strong Willed Mage " + username;
Console.WriteLine(Message);
}
if (Userclass.Contains("Warrior"))
{
String Message = "Your are a valiant Warrior " + username;
Console.WriteLine(Message);
}
if (Userclass.Contains("Assasin"))
{
String Message = "You are a vigilant Assasin " + username;
Console.WriteLine(Message);
}
Console.ReadLine();
}
however if i define:
string Userclass = Console.Readline();
I get the error that it
can't convert string[] to string
thanks for any help!
Upvotes: 3
Views: 3135
Reputation: 1095
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;
}
Upvotes: 3
Reputation: 531
Here you go, I wrote comments explaining each part of the program, let me know if you can't understand something.
static void Main(string[] args)
{
Console.Write("Enter Username: "); // we ask for the username here, you can do it with Console.WriteLine() as well
string username = Console.ReadLine(); // get the username
Console.Write("What class will you be? You can choose from Mage, Warrior, Or Assasin: "); // ask for the class, also same as above, you can use Console.WriteLine()
string userclass = Console.ReadLine(); // get the class
// Possible character classes (we use this as a reference to check the class the user gives us later)
// also set the string representing each class as lower case characters to avoid looping or unnecessary string concatenation later
string[] charClasses = new string[3] { "mage", "warrior", "assasin" }; // make sure the class given is within our possible character classes
// here we check that the class given is within the bound of our possible classes and convert
// the userclass to its lower case equivalent to match it with those in the array
if (Array.IndexOf(charClasses, userclass.ToLower()) != -1)
{
// if the class is acceptable then attach it to the message along with the username via string concatenation
String Message = "You are a strong Willed "+userclass + " " + username;
Console.WriteLine(Message); // print the message
}
}
Upvotes: 2