【发布时间】:2019-04-18 17:23:55
【问题描述】:
因此,在我学习 C# 的过程中,我正在尝试创建一个交互式故事,该故事会根据用户提交的某些输入进行更改。如果在这种情况下用户输入“Bobby”,叙述者就会开始像 Hank Hill 一样说话。但是,根据它的编程方式,输入是区分大小写的。
我已经尝试过我看到的一件事建议,就是将 if 语句格式化为:
if (boyName.ToUpper() == "Bobby")
但这并没有触发不同字母大小写的 if 命令
Console.WriteLine($"{beginning} \n What was the boy's name?");
boyName = Console.ReadLine();
if (boyName == "Bobby")
{
Console.WriteLine("That boy ain\'t right, I tell ya what... ");
Console.ReadKey();
Console.WriteLine($"{boyName} boy dang climbed a big ol' tree...");
Console.ReadKey();
}
else
{
Console.WriteLine($"The kid named {boyName} climbed a tree...");
Console.ReadKey();
}
我希望有一行代码可以在任何情况下触发 if 条件。但是,我尝试的一切并没有改变这一点。它需要专门为“Bobby”,否则会触发 else 条件
【问题讨论】:
-
boyName.ToUpper() == "BOBBY" -
欢迎来到 StackOverFlow @DaimonCide,也许您可以尝试将 if 语句的两边都大写,如
if (boyName.ToUpper() == "Bobby".ToUpper())或将 ifif (boyName.ToLower() == "Bobby".ToLower())的两边小写 -
嗨。也许你知道 .ToUpper 函数不正确。此函数不会使字符串驼峰式大小写,而是使所有字母大写。
标签: c# string case-sensitive uppercase lowercase