我无法弄清楚如何将开关与枚举结合使用。你能告诉我我做错了什么,以及如何解决它?我必须使用枚举来制作基本的计算器。
public enum Operator
{
PLUS, MINUS, MULTIPLY, DIVIDE
}
public double Calculate(int left, int right, Operator op)
{
int i = (int) op;
switch(i)
{
case 0:
{
return left + right;
}
case 1:
{
return left - right;
}
case 2:
{
return left * right;
}
case 3:
{
return left / right;
}
default:
{
return 0.0;
}
}
}
最终结果应该是这样的:
Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, PLUS))
Output: The sum of 5 and 5 is 10
你能告诉我我是怎么弄乱的吗?
你不需要转换它
switch(op)
{
case Operator.PLUS:
{
// your code
// for plus operator
break;
}
case Operator.MULTIPLY:
{
// your code
// for MULTIPLY operator
break;
}
default: break;
}
顺便说一句,使用括号
无需转换。您可以在交换机内的Enum上应用条件。像这样,
public enum Operator
{
PLUS,
MINUS,
MULTIPLY,
DIVIDE
}
public double Calculate(int left, int right, Operator op)
{
switch (op)
{
case Operator.PLUS: return left + right;
case Operator.MINUS: return left - right;
case Operator.MULTIPLY: return left * right;
case Operator.DIVIDE: return left / right;
default: return 0.0;
}
}
然后,这样称呼它:
Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, Operator.PLUS));
已经给出了正确的答案,不过这里有更好的方法(比开关):
private Dictionary<Operator, Func<int, int, double>> operators =
new Dictionary<Operator, Func<int, int, double>>
{
{ Operator.PLUS, ( a, b ) => a + b },
{ Operator.MINUS, ( a, b ) => a - b },
{ Operator.MULTIPLY, ( a, b ) => a * b },
{ Operator.DIVIDE ( a, b ) => (double)a / b },
};
public double Calculate( int left, int right, Operator op )
{
return operators.ContainsKey( op ) ? operators[ op ]( left, right ) : 0.0;
}
根本就不要转换为int
switch(operator)
{
case Operator.Plus:
//todo
你不应该转换为整数。对于除法,你需要先左转加倍,否则你会做一个整数除法。
public enum Operator
{
PLUS, MINUS, MULTIPLY, DIVIDE
}
public double Calculate(int left, int right, Operator op)
{
double sum = 0.0;
switch(op)
{
case Operator.PLUS:
sum = left + right;
return sum;
case Operator.MINUS:
sum = left - right;
return sum;
case Operator.MULTIPLY:
sum = left * right;
return sum;
case Operator.DIVIDE:
sum = (double)left / right;
return sum;
default:
return sum;
}
return sum;
}
public enum Operator
{
PLUS, MINUS, MULTIPLY, DIVIDE
}
public class Calc
{
public void Calculate(int left, int right, Operator op)
{
switch (op)
{
case Operator.DIVIDE:
//Divide
break;
case Operator.MINUS:
//Minus
break;
case Operator.MULTIPLY:
//...
break;
case Operator.PLUS:
//;;
break;
default:
throw new InvalidOperationException("Couldn't process operation: " + op);
}
}
}
如果您不想对每种情况使用return语句,请尝试以下操作:
Calculate(int left, int right, Operator op)
{
int result = 0;
switch(op)
{
case Operator.PLUS:
{
result = left + right;;
}
break;
....
}
return result;
}
所有其他答案都是正确的,但您还需要正确调用您的方法:
Calculate(5, 5, Operator.PLUS))
而且因为你使用int
用于left
和right
,结果将是int
(3/2 will result in 1
)。您可以在计算结果之前强制转换为double
或修改您的参数以接受double
两件事情。首先,您需要在测试中限定枚举引用 - 而不是“PLUS”,它应该是“Operator.PLUS”。其次,如果您使用枚举成员名称而不是switch语句中的整数值,则此代码将更具可读性。我已经更新了你的代码:
public enum Operator
{
PLUS, MINUS, MULTIPLY, DIVIDE
}
public static double Calculate(int left, int right, Operator op)
{
switch (op)
{
default:
case Operator.PLUS:
return left + right;
case Operator.MINUS:
return left - right;
case Operator.MULTIPLY:
return left * right;
case Operator.DIVIDE:
return left / right;
}
}
称之为:
Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, Operator.PLUS));
你的代码很好。如果您不确定如何使用计算功能,请尝试
Calculate(5,5,(Operator)0); //this will add 5,5
Calculate(5,5,Operator.PLUS);// alternate
默认枚举值从0开始,对于后续元素增加1,直到您指定不同的值。你也可以这样做:
public enum Operator{PLUS=21,MINUS=345,MULTIPLY=98,DIVIDE=100};