1

所以我试图做的是让这个程序在输入时忽略用户的字母大小写。我知道如何使用.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!");
            }





        }
    }
}

}

4

5 回答 5

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 语句中的一种情况。

于 2014-04-25T20:44:40.760 回答
3

你只需要这样做:

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

你不需要那个 nested if,你可以删除它。你还必须在 if 块的末尾添加一个 break ,所以它会在用户输入一个有效值后打破你的循环,否则循环将永远不会结束。

于 2014-04-25T20:44:47.980 回答
3

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

Enum.TryParse(Console.ReadLine(), true, out favorite);
于 2014-04-25T20:45:36.327 回答
2

请注意,这种重载允许您忽略输入字符串的TryParse大小写,因此您可以将其写为:

if (Enum.TryParse(Console.ReadLine(), true, out favorite))
于 2014-04-25T20:46:01.563 回答
1

String.ToLower()将以小写形式返回字符串值。

if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))
于 2014-04-25T20:44:49.580 回答