[英]How to check if an input is equal to a string
I am trying to make a friendly ai, but I need to be able to detect if the input is equal to a string.我正在尝试制作一个友好的 ai,但我需要能够检测输入是否等于字符串。 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 :)谢谢你的帮助 :)
if(enteredCommand == "1")
由于Console.ReadLine
返回一个字符串,您还需要将其与字符串进行比较,而不是整数
I suggest you to stick to我建议你坚持
string.Equals(string1, string2);
for comparing strings in the future, since it is less type strict .用于将来比较字符串,因为它的类型不那么严格。 (ie you can compare object obj = "string" and string str = "string" using .Equals and you will get true , while using "==" the result will be false .
(即您可以使用.Equals比较object obj = "string"和string str = "string" ,您将得到true ,而使用"=="结果将为false 。
PS. 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.