string usertype;
usertype = Console.ReadLine();
if (usertype== "Yahoo")
{
Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page");
Console.ReadLine();
}
代码没有错,除了:如果用户输入 Y ahoo,则会显示答案。我想要用户;如果他输入 y ahoo,那么答案应该是相同的。
答案 0 :(得分:2)
string usertype;
usertype = Console.ReadLine();
if (string.Equals(usertype,"Yahoo",StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page");
Console.ReadLine();
}
答案 1 :(得分:0)
您可以使用String.ToLower()方法使用类似的代码:
string usertype;
usertype = Console.ReadLine();
if (usertype.ToLower() == "yahoo")
{
Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page");
}
答案 2 :(得分:0)
使用String.Equals方法,而不是使用“==”运算符进行比较。
如果您需要不区分大小写的比较,请使用System.StringComparison.OrdinalIgnoreCase
StringComparison枚举。
MSDN的说明:
basic ordinal comparison (System.StringComparison.Ordinal) is case-sensitive, which means that the two strings must match character for character: "and" does not equal "And" or "AND". A frequently-used variation is System.StringComparison.OrdinalIgnoreCase, which will match "and", "And", and "AND". StringComparison.OrdinalIgnoreCase is often used to compare file names, path names, network paths, and any other string whose value does not change based on the locale of the user's computer. For more information
可以找到更多信息here
在你的情况下,我会使用以下条件:
if (usertype.Equals("yahoo", StringComparison.OrdinalIgnoreCase))
//then do whatever you want...
另一个选择是小写所有内容,但第一个选项是首选。
if (usertype.ToLower() == "yahoo")
//then do whatever you want...