【问题标题】:How to use .ToLower() properly in this case?在这种情况下如何正确使用 .ToLower() ?
【发布时间】:2014-04-25 20:42:41
【问题描述】:

所以我试图做的是让这个程序在输入时忽略用户的字母大小写。我看怎么用.ToLower();但是我不明白如何以正确的方式做到这一点。

这就是我现在所拥有的,我接近了吗?我在网上阅读了很多教程,但是它们大多只是将用户输入转换为较低的独立程序。有没有办法在全球范围内启用它?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Choose_Your_Color
{
class Program
{
    enum Color
    {
        red,
        orange,
        blue,
        black,
        white,
        green,
        purple,
        yellow
    }
    static void Main(string[] args)
    {

        Color favorite;
        while (true)
        {
            Console.WriteLine("What color do you choose?");
            if (Enum.TryParse(Console.ReadLine(), out favorite))
                if (string.compare(favorite , Enum , true) == 0){
                    Continue;
                }



            {
                switch (favorite)
                {
                    case Color.red:
                        Console.WriteLine("You chose red!");
                        break;
                    case Color.orange:
                        Console.WriteLine("you chose orange!!!!!!");
                        break;
                    case Color.blue:
                        Console.WriteLine("YOU CHOSE BLUEEEE!!");
                        break;
                    case Color.black:
                        Console.WriteLine("you chose black");
                        break;
                    case Color.white:
                        Console.WriteLine(" you chose white!");
                        break;
                    case Color.green:
                        Console.WriteLine("you chose green!!!!!");
                        break;
                    case Color.purple:
                        Console.WriteLine("you chose purple!!");
                        break;
                    case Color.yellow:
                        Console.WriteLine("you chose yellow!!!");
                        break;

                }
            }

            else
            {
                Console.WriteLine("That's not a color!");
            }





        }
    }
}

}

【问题讨论】:

  • 感谢指正。

标签: c# string string-comparison tolower


【解决方案1】:

你只需要这样做:

if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))

您不需要嵌套的if,只需将其删除即可。此外,您还必须在 if 块的末尾添加一个中断,这样在用户输入有效值后它将中断您的循环,否则循环永远不会结束。

【讨论】:

    【解决方案2】:

    Enum.TryParse接受参数忽略大小写:

    Enum.TryParse(Console.ReadLine(), true, out favorite);
    

    【讨论】:

      【解决方案3】:

      只要改变这个;

      if (Enum.TryParse(Console.ReadLine(), out favorite))
      

      if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))
      

      Console.ReadLine() 返回一个字符串,这将调用该值的 lower,确保所有输入都是小写的。

      为什么会有这条线?

                  if (string.compare(favorite , Enum , true) == 0){
                      Continue;
                  }
      

      我认为没有任何理由。 Enum.TryParse 应该返回 false 表示输入不是枚举之一,并且您不会进入 switch 语句或 favorite 将是枚举值之一,您将进入您的案例之一switch 语句。

      【讨论】:

      • 嗯,它仍然返回它,因为它没有被识别。
      • 这是因为while循环的原因吗?
      • @user3554677 也许输入与任何枚举值都不匹配?您可能还想在输入上调用Trim(),因为它可能包含会阻止匹配的空格。另外,我将在我的答案中编辑最后一件事。
      • 我删除了第二个 if 语句。代码与减去第二个 if 语句并添加“if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))”完全一样
      • @user3554677 在这种情况下,您编写的代码与 msdn 上的示例非常相似。你能在 if 的主体中设置一个断点,看看发生了什么吗? Enum.TryParse 的使用是正确的,所以我不确定是什么导致了问题。
      【解决方案4】:

      请注意,TryParsethis overload 允许您忽略输入字符串的大小写,因此您可以将其写为:

      if (Enum.TryParse(Console.ReadLine(), true, out favorite))
      

      【讨论】:

        【解决方案5】:

        String.ToLower() 会将字符串值返回为小写。

        if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))
        

        【讨论】:

          猜你喜欢
          • 2020-08-06
          • 2022-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-02
          • 2013-11-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多