请问,我该如何重构这段代码以避免切换情况?我需要改用字典吗?有没有一种方法可以在不添加新案例的情况下更改命令并自动生成...我的意思是这个解决方案不会取决于案例的数量。我希望我的问题很清楚。
static void Main(string[] args)
{
ConsoleHelp ch1 = new ConsoleHelp();
Plus pl = new Plus();
Minus mn = new Minus();
Divide dv = new Divide();
Multiply mlt = new Multiply();
Sinus sin = new Sinus();
Tangent tan = new Tangent();
Square sq = new Square();
Degree dg = new Degree();
Percent pr = new Percent();
while (true)
{
Console.Write("command> ");
string com = Console.ReadLine();
if (com != null)
{
switch (com.ToLower())
{
case "?":
ch1.Helpper();
break;
case "plus":
pl.Sum();
break;
case "minus":
mn.Minusvalue();
break;
case "divide":
dv.Dividevalue();
break;
case "multiply":
mlt.Multvalue();
break;
case "sinus":
sin.Sinusvalue();
break;
case "tangent":
tan.Tangentvalue();
break;
case "square":
sq.Squarevalue();
break;
case "degree":
dg.Degvalue();
break;
case "percent":
pr.Pervalue();
break;
case "c":
Environment.Exit(0);
break;
default:
Console.WriteLine("The command is not supported. Enter one of the valid commands.");
break;
}
}
}
Console.ReadLine();
}