2016-12-04 55 views
1

我正在使用下面的代码根据使用switch语句的用户输入类型执行转换。是否可以在这里使用“enum”和“struct”而不是switch语句?枚举和结构代替C语言中的switch语句的用法

using System; 
using static System.Console; 
using static System.Convert; 

namespace Problem 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      double centimeter, liters, kilometer, kilogram; 
      WriteLine("Enter the value you wanted to convert: "); 
      int input = ToInt32(ReadLine()); 
      WriteLine("\n Press Any Of The Given Choices \n I->convert from inches to centimeters." + 
         "\n G->convert from gallons to liters.\n M->convert from mile to kilometer."+ 
         "\n P->convert from pound to kilogram."); 
      char choice = Char.ToLower(ToChar(ReadLine())); 
      switch (choice) 
      { 
       case 'i': 
        centimeter = input/0.3937;      //1 cm is equal is 0.3037 inch 
        WriteLine($"In Centimeters: {centimeter}"); 
        break; 
       case 'g': 
        liters= input * 3.78;        // 1 gallon=3.78 litters 
        WriteLine($"In Liters: { liters}"); 
        break; 
       case 'm': 
        kilometer = input *1.60;       // 1 mile=1.4 KM 
        WriteLine($"In kilometer: { kilometer}"); 
        break; 
       case 'p': 
        kilogram = input * 0.453;       // 
        WriteLine($"In kilogram: { kilogram}"); 
        break; 
       default: 
        WriteLine("You Enter A Invalid Choice, Please Enter A Valid Choice...!"); 
        ReadLine(); 
        break; 
        } 
      ReadKey(); 
     } 
    } 
} 

回答

2

您可以使用Dictionary代替switch声明。
对于您可以创建界面

public interface IConverter 
{ 
    string GetFormattedResult(int value); 
} 

然后创建为每个单位自己的实现必须

public class CentimeterConverter : IConverter 
{ 
    private const double COEFFICENT = 0.3937; 
    public string GetFormattedResult(int value) 
    { 
     var centimeter = input/COEFFICENT ; 
     return $"In Centimeters: {centimeter}"; 
    } 
} 
在你的代码

然后创建字典与重点更清洁的方式 - 选择焦炭和价值 - 转换器的实例实施

static void Main(string[] args) 
{ 
    var converters = new Dictionary<char, IConverter> 
    { 
     { 'i', new CentimeterConverter() }, 
     { 'g', new LitersConverter() } 
    } 

    WriteLine("Enter the value you wanted to convert: "); 
    int input = ToInt32(ReadLine()); 

    var choiceText = 
     "Press Any Of The Given Choices 
     I->convert from inches to centimeters. 
     G->convert from gallons to liters. 
     M->convert from mile to kilometer. 
     P->convert from pound to kilogram."; 
    WriteLine(choiceText); 

    char choice = Char.ToLower(ToChar(ReadLine())); 

    var converter = converters[choice]; 
    WriteLine(converter.Convert(input)); 

    ReadKey(); 
} 

为了使键很少出价更具可读性,您可以使用静态类与常量

public static class ConverterKayes 
{ 
    public const char InchesToCentimaters = 'i'; 
    public const char GallonsToLiters = 'g'; 
} 

枚举是整数类型,这样你就可以创建枚举,这里的名字将钥匙

public enum Keys 
{ 
    I = 1, 
    G = 2 
} 

但这枚举没有给出更多的价值,你的代码的可读性。

如果你真的想枚举,那么你可以使用DescriptionAttribute在那里你可以在属性

public enum Keys 
{ 
    [Description("I")] InchesToCentimeters = 1, 
    [Description("G")] GallonsToLiters = 2 
} 

定义键但您需要创建一些方法来从属性检索值。 同系数 - 它很容易保存为静态类的常量。

另一种方法可以KeyedCollection,也可以是适合你的情况,因为你必须为所有的密钥相同的逻辑,只值改变

public class ConverterToContinentalUnit 
{ 
    public char Key { get; set; } 
    public double Coefficent { get; set; } 
    public string PrefixForResult { get; set; } 

    public string GetFormattedResult(int value) 
    { 
     var continentalUnit = input/Coefficent; 
     return $"{PrefixForResult}: {continentalUnit}"; 
    } 
} 

public class ConverterCollection: KeyedCollection<int, ConverterToContinentalUnit> 
{ 
    // This need to be implemented and return key value 
    protected override int GetKeyForItem(ConverterToContinentalUnit item) 
    { 
     return item.Key ; 
    } 
} 

然后用它

static void Main(string[] args) 
{ 
    var converters = new ConverterCollection(); 
    var toCentimeters = new ConverterToContinentalUnit 
    { 
     Key = "i", 
     Coefficent = 0.3937, 
     PrefixForResult = "In Centimeters" 
    } 

    converters.Add(toCentimeters); 

    WriteLine("Enter the value you wanted to convert: "); 
    int input = ToInt32(ReadLine()); 

    var choiceText = 
     "Press Any Of The Given Choices 
     I->convert from inches to centimeters. 
     G->convert from gallons to liters. 
     M->convert from mile to kilometer. 
     P->convert from pound to kilogram."; 
    WriteLine(choiceText); 

    char choice = Char.ToLower(ToChar(ReadLine())); 

    var converter = converters[choice]; 
    WriteLine(converter.Convert(input)); 

    ReadKey(); 
} 
0

枚举支持“字节,sbyte,short,ushort,int,uint,long或ulong“。它不支持double或float。但是你可以为它使用结构。

using System; 
using static System.Console; 
using static System.Convert; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     public struct MyUnits 
     { 
      public const double Centimeter = 0.3037; 
      public const double Liters = 3.78; 
      public const double Kilometer = 1.60; 
      public const double Kilogram = 0.453; 
     } 

     enum MyUnitEnum 
     { 
      Centimeters, 
      Liters, 
      Kilometer, 
      Kilogram, 
     } 

     static void Main(string[] args) 
     { 
      double centimeter, liters, kilometer, kilogram, final = 0; 
      string unitWord; 
      WriteLine("Enter the value you wanted to convert: "); 
      int input = ToInt32(ReadLine()); 
      WriteLine("\n Press Any Of The Given Choices \n I->convert from inches to centimeters." + 
         "\n G->convert from gallons to liters.\n M->convert from mile to kilometer." + 
         "\n P->convert from pound to kilogram."); 

      char choice = Char.ToLower(ToChar(ReadLine())); 

      if (choice == 'i') 
      { 
       final = input/MyUnits.Centimeter; 
       unitWord = MyUnitEnum.Centimeters.ToString(); 
      } 
      else if(choice == 'g') 
      { 
       final = input/MyUnits.Liters; 
       unitWord = MyUnitEnum.Liters.ToString(); 

      } 
      else if (choice == 'm') 
      { 
       final = input/MyUnits.Kilometer; 
       unitWord = MyUnitEnum.Kilometer.ToString(); 
      } 
      else if (choice == 'p') 
      { 
       final = input/MyUnits.Kilogram; 
       unitWord = MyUnitEnum.Kilogram.ToString(); 
      } 
      else 
      { 
       unitWord = "You Enter A Invalid Choice, Please Enter A Valid Choice...!"; 
      } 

      WriteLine("In " + unitWord + final); 
      ReadKey(); 
     } 
    } 
}