c# - 使用 int、strings 和 chars 使方程式 int
问题描述
我正在尝试做这样的事情。
string operator = "+";
int randomNumber = 1;
int randomNumber2=2;
if(randomNumber+operator+randomNumber2 == 3)
但似乎无法让它工作,我也尝试做一个没有运气的字符。
解决方案
你分享的例子是完全错误的,一个建议你的问题:
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");
}
}
推荐阅读
- javascript - 如何从返回数组的 web 服务中获取数据
- javascript - 解构和克隆对象/一行?
- c++ - 提升,以分钟精度获得 UTC 时间
- string - "${line//END}" != "$line" - 它有什么作用
- responsive-design - 有谁知道一个可以作为 epub 工作的 JS 库
- python - ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)。试图预测特斯拉股票
- flutter - 轮播时间(秒)随图像变化而变化
- javascript - 集合中标签的递归
- node.js - 获取 mongo 集合中与交集数组中的任何元素匹配的所有文档
- keras - 不匹配形状的 Keras 层级联