C# how to use enum with switch
我不知道如何将开关与枚举结合使用。你能告诉我我做错了什么,怎么修理吗?我必须使用枚举来制作一个基本的计算器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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; } } } |
最终结果应该是这样的:
1 2 |
你们能告诉我我是怎么搞砸的吗?
你不需要转换它
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | switch(op) { case Operator.PLUS: { // your code // for plus operator break; } case Operator.MULTIPLY: { // your code // for MULTIPLY operator break; } default: break; } |
顺便说一下,用括号
已经给出了正确的答案,不过这里有更好的方法(而不是切换):
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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
1 2 3 4 | switch(operator) { case Operator.Plus: //todo |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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); } } } |
不应强制转换为整数。对于除法,首先需要将左键强制转换为双精度,否则将执行整数除法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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; } |
如果不想对每种情况使用RETURN语句,请尝试以下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Calculate(int left, int right, Operator op) { int result = 0; switch(op) { case Operator.PLUS: { result = left + right;; } break; .... } return result; } |
所有其他答案都是正确的,但您还需要正确调用您的方法:
1 | Calculate(5, 5, Operator.PLUS)) |
因为你用
不需要转换。可以对开关内的枚举应用条件。像这样,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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; } } |
然后,这样称呼它:
1 | Console.WriteLine("The sum of 5 and 5 is" + Calculate(5, 5, Operator.PLUS)); |
你的密码没问题。如果您不确定如何使用计算函数,请尝试
1 2 | 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}; |
两件事。首先,您需要在测试中限定枚举引用,而不是"加号",它应该是"operator.plus"。第二,如果在switch语句中使用枚举成员名而不是其整数值,则此代码的可读性将大大提高。我已经更新了您的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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; } } |
用以下方式调用:
1 | Console.WriteLine("The sum of 5 and 5 is" + Calculate(5, 5, Operator.PLUS)); |