如何使用用户输入从枚举中获取输出? C#

时间:2018-01-31 03:14:41

标签: enums

我写了这段代码,要求用户输入枚举中的一个值,但我不知道如何将枚举值连接到用户输入。请帮忙。

谢谢!

    class Program
    {
        static void Main(string[] args)
        {

            System.Console.WriteLine("Hello, please enter the numerical value corresponding to your transportation mode.Car = 10 / Bus = 20 / Bicycle = 5 / Cab = 7.");
            int TransportationModeInput = int.Parse(Console.ReadLine());

        public enum TransporationMode
        {
            Car = 10,
            Bus = 20,
            Bicycle = 5,
            Cab = 7
        }

        int ModeCar = (int)TransporationMode.Car;
        int ModeBus = (int)TransporationMode.Bus;
        int ModeBicycle = (int)TransporationMode.Bicycle;
        int ModeCab = (int)TransporationMode.Cab;



  }
    }

1 个答案:

答案 0 :(得分:0)

(TransporationMode)TransportationModeInput 自动转换为枚举

public enum TransporationMode
{
    Car = 10,
    Bus = 20,
    Bicycle = 5,
    Cab = 7
}

class Program
{
    static void Main(string[] args)
    {
        System.Console.WriteLine("Hello, please enter the numerical value corresponding to your transportation mode.Car = 10 / Bus = 20 / Bicycle = 5 / Cab = 7.");
        int TransportationModeInput = int.Parse(Console.ReadLine());

        Console.WriteLine("Answer:" + ((TransporationMode)TransportationModeInput).ToString());
        Console.ReadLine();
    }
}