C#如何使用带开关的枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15136134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# how to use enum with switch
提问by yesman
I can't figure out how to use switches in combination with an enum. Could you please tell me what I'm doing wrong, and how to fix it? I have to use an enum to make a basic calculator.
我不知道如何将开关与枚举结合使用。你能告诉我我做错了什么,以及如何解决它?我必须使用枚举来制作一个基本的计算器。
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;
}
}
}
The end result should be something like this:
最终结果应该是这样的:
Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, PLUS))
Output: The sum of 5 and 5 is 10
Could you guys please tell me how I'm messing up?
你们能告诉我我是怎么搞砸的吗?
采纳答案by J.Starkl
You don't need to convert it
你不需要转换它
switch(op)
{
case Operator.PLUS:
{
// your code
// for plus operator
break;
}
case Operator.MULTIPLY:
{
// your code
// for MULTIPLY operator
break;
}
default: break;
}
By the way, use brackets
顺便说一句,使用括号
回答by Jeow Li Huan
You should not cast to integer. And for the division, you need to cast left to double first, if not you will be doing an integer divide.
您不应该强制转换为整数。对于除法,您需要先将 left 转换为 double,否则您将进行整数除法。
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;
}
回答by burning_LEGION
simply don't cast to int
只是不要强制转换为 int
switch(operator)
{
case Operator.Plus:
//todo
回答by Stephan Bauer
All the other answers are correct, but you also need to call your method correctly:
所有其他答案都是正确的,但您还需要正确调用您的方法:
Calculate(5, 5, Operator.PLUS))
And since you use int
for left
and right
, the result will be int
as well (3/2 will result in 1
). you could cast to double
before calculating the result or modify your parameters to accept double
并且由于您使用int
forleft
和right
,结果也将是int
( 3/2 will result in 1
)。您可以double
在计算结果之前强制转换为或修改您的参数以接受double
回答by omer schleifer
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);
}
}
}
回答by Mike
Two things. First, you need to qualify the enum reference in your test - rather than "PLUS", it should be "Operator.PLUS". Second, this code would be a lot more readable if you used the enum member names rather than their integral values in the switch statement. I've updated your code:
两件事情。首先,您需要在测试中限定枚举引用 - 而不是“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;
}
}
Call this with:
调用它:
Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, Operator.PLUS));
回答by JustAndrei
The correct answer is already given, nevertheless here is the better way (than switch):
正确答案已经给出,但这里有更好的方法(比 switch 更好):
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;
}
回答by novic3
Your code is fine. In case you're not sure how to use Calculate function, try
你的代码没问题。如果您不确定如何使用计算功能,请尝试
Calculate(5,5,(Operator)0); //this will add 5,5
Calculate(5,5,Operator.PLUS);// alternate
Default enum values start from 0 and increase by one for following elements, until you assign different values. Also you can do :
默认枚举值从 0 开始,并为后续元素增加 1,直到您分配不同的值。你也可以这样做:
public enum Operator{PLUS=21,MINUS=345,MULTIPLY=98,DIVIDE=100};
回答by Martin
In case you don't want to use return statement for each case, try this:
如果您不想为每种情况使用 return 语句,请尝试以下操作:
Calculate(int left, int right, Operator op)
{
int result = 0;
switch(op)
{
case Operator.PLUS:
{
result = left + right;;
}
break;
....
}
return result;
}
回答by Kent Aguilar
No need to convert. You can apply conditions on Enums inside a switch. Like so,
无需转换。您可以在开关内的枚举上应用条件。像这样,
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;
}
}
Then, call it like this:
然后,像这样调用它:
Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, Operator.PLUS));