首页 > 解决方案 > 使用 int、strings 和 chars 使方程式 int

问题描述

我正在尝试做这样的事情。

string operator = "+";
int randomNumber = 1;
int randomNumber2=2;

if(randomNumber+operator+randomNumber2 == 3)

但似乎无法让它工作,我也尝试做一个没有运气的字符。

标签: c#

解决方案


你分享的例子是完全错误的,一个建议你的问题:

  public static int Operator(this string logic, int x, int y)
    {
        switch (logic)
        {
            case "+": return x + y;
            case "-": return x - y;
            // And add your own cases

            default: throw new Exception("invalid logic");
        }
    }

推荐阅读