93

我不知道如何将开关与枚举结合使用。你能告诉我我做错了什么,以及如何解决吗?我必须使用枚举来制作基本的计算器。

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

你们能告诉我我是怎么搞砸的吗?

4

11 回答 11

147

你不需要转换它

switch(op)
{
     case Operator.PLUS:
     {
        // your code 
        // for plus operator
        break;
     }
     case Operator.MULTIPLY:
     {
        // your code 
        // for MULTIPLY operator
        break;
     }
     default: break;
}

顺便说一句,使用括号

于 2013-02-28T12:58:27.600 回答
29

由于 C# 8.0 为枚举引入了一个新的 switch 表达式,你可以做得更优雅:

public double Calculate(int left, int right, Operator op) =>
            op switch 
        {
            Operator.PLUS => left + right,
            Operator.MINUS => left - right,
            Operator.MULTIPLY => left * right,
            Operator.DIVIDE => left / right,
            _    =>  0
        }

参考。https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

于 2019-10-09T05:53:00.960 回答
11

正确答案已经给出,但这里是更好的方法(比 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;
}
于 2013-02-28T13:07:45.043 回答
3

您不应该转换为整数。对于除法,您需要先将 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;
}
于 2013-02-28T12:59:41.290 回答
3

根本不强制转换为 int

 switch(operator)
    {
       case Operator.Plus:
       //todo
于 2013-02-28T12:59:55.450 回答
3

无需转换。您可以在开关内的枚举上应用条件。像这样,

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));
于 2017-01-11T04:38:51.080 回答
2
 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);
            }
        }
    }
于 2013-02-28T13:02:35.997 回答
1

所有其他答案都是正确的,但您还需要正确调用您的方法:

Calculate(5, 5, Operator.PLUS))

并且由于您使用intforleftright,结果也将是int( 3/2 will result in 1)。您可以double在计算结果之前强制转换或修改参数以接受double

于 2013-02-28T13:02:03.623 回答
1

如果您不想为每种情况使用 return 语句,请尝试以下操作:

Calculate(int left, int right, Operator op)
{
   int result = 0;
   switch(op)
   {
        case Operator.PLUS:
        {
            result = left + right;;  
        }
        break;
        ....
   }

   return result;
}
于 2014-10-03T09:27:04.803 回答
0

两件事情。首先,您需要在测试中限定枚举引用 - 而不是“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));
于 2013-02-28T13:03:51.713 回答
0

你的代码很好。如果您不确定如何使用计算功能,请尝试

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};
于 2013-02-28T13:16:51.713 回答