gpt4 book ai didi

c# - 将字符串转换为运算符?

转载 作者:行者123 更新时间:2023-11-30 14:13:33 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Is there a string math evaluator in .NET?

有没有一种方法可以在不检查 "+"、"-"、"*"、"/" 然后传递值的情况下转换运算符代码?

using System;

namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
string operator = "+";
int inputOne = 2;
int inputTwo = 5;

Console.WriteLine(Result(inputOne, inputTwo, operator));
Console.ReadLine();
}


public static string Result(int one, int two, string computeOperator)
{
if (computeOperator == "+")
{
return (one + two).ToString();
}

if (computeOperator == "-")
{
return (one - two).ToString();
}

//and so on...

return "0";
}
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com