我正在使用下面的代碼根據使用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();
}
}
}