用于使用枚举(C#)调用方法的用户输入

时间:2016-04-01 16:43:06

标签: c# enums compiler-errors user-input

我刚开始使用C#而且我遇到了问题。我有一个问题我需要回答需要使用枚举,并需要使用用户输入来触发切换。到目前为止,我几乎已经知道了所有这些,但我发现了一个不会消失的错误。我还没有在C#中学到足够的知识来解决这个问题,所以我真的需要帮助。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;    

namespace enumExample
{
    class Program
    {
        public enum Operator
        {
            add,
            subtract,
            multiply,
            divide
        }

        static void Main(string[] args)
        {
            System.Console.WriteLine("Please enter an integer");
            string leftstring = System.Console.ReadLine();
            int left = System.Convert.ToInt32(leftstring);

            System.Console.WriteLine("Please enter another integer");
            string rightstring = System.Console.ReadLine();
            int right = System.Convert.ToInt32(rightstring);

            System.Console.WriteLine("Please enter '0' to add, '1' to subtract, '2' to multiply, and '3' to divide");
            string op = System.Console.ReadLine();
            int x = System.Convert.ToInt32(op);

            double sum = Calculate(left, right, Operator.op);
            System.Console.WriteLine("The answer to your problem is {0}", sum);
            System.Console.ReadLine();
        }

        public static double Calculate(int left, int right, Operator op)
        {
            double sum = 0.0;

            switch (op)
            {
                case Operator.add:
                    sum = left + right;
                    break;
                case Operator.subtract:
                    sum = left - right;
                    break;
                case Operator.multiply:
                    sum = left * right;
                    break;
                case Operator.divide:
                    sum = (double)left / right;
                    break;
                default:
                    System.Console.WriteLine("An error occurred");
                    System.Console.ReadLine();
                    break;
            }

            return sum;
        }
   }
}

当我去编译我的程序时,我收到了这个错误:

Code: CS0117
Description: 'Program.operator' does not contain a definition for 'op'
Project: enumExample
File: Program.cs
Line: 33
Suppression State: Active

感谢您抽出宝贵时间阅读本文,并提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您的运营商枚举没有名为“op”的值

在第33行,你有:

double sum = Calculate(left, right, Operator.op);

我猜你想要写的是

double sum = Calculate(left, right, (Operator)x);

这会将你的int转换为正确的枚举值。

如果你想做更多这类事情,你可能要考虑明确设置每个枚举值的值。

public enum Operator
    {
        add = 0,
        subtract = 1,
        multiply = 2,
        divide = 3
    };

答案 1 :(得分:0)

你在这里得到了这个:

double sum = Calculate(left, right, Operator.op);

你说,Operator.op。但你的枚举包含:

    public enum Operator
    {
        add,
        subtract,
        multiply,
        divide
        op <----- You don't have one of these, which you'd have to in order to pass in Operator.op.
    }

也许你的意思是double sum = Calculate(left, right, x);(即 x ,而不是Operator.op。