I am trying to make a friendly ai, but I need to be able to detect if the input is equal to a string. I tried a lot of things, but none of them worked.
Here is my code so far... ;)
namespace Game
{
public static class Program
{
public static string name;
public static string enteredCommand;
public static int commanddomath = 1;
static void Main()
{
Console.Title = "Phil";
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" ");
Console.WriteLine(" / / / /");
Console.WriteLine(" |@| |@|");
Console.WriteLine(" --_____-- ");
Console.WriteLine(" ////// ");
Console.WriteLine(" ");
Console.WriteLine("Hello, my name is Phil");
Console.WriteLine("What is your name");
name = Console.ReadLine();
Console.WriteLine("Hello, " name);
// Console.WriteLine("What do you want me to do?");
// enteredCommand = Console.ReadLine();
// if (enteredCommand = 1)
// {
// }
Console.ReadKey();
}
}
}
Thanks for any help :)
CodePudding user response:
if(enteredCommand == "1")
As Console.ReadLine
returns a string, you will also need to compare it to a string, not an integer
CodePudding user response:
I suggest you to stick to
string.Equals(string1, string2);
for comparing strings in the future, since it is less type strict. (i.e. you can compare object obj = "string" and string str = "string" using .Equals and you will get true, while using "==" the result will be false.
PS. Don't use it when type safety is necessary.
It also accepts
System.StringComparison.OrdinalIgnoreCase
Which ignores casing for strings/objects.
if (string.Equals(enteredCommand, "1"))
{
//do something
}