如何串联用户选择的操作员,并分配给该操作员?

时间:2019-07-04 09:49:15

标签: c# concatenation

我是C#的新手,但是我可以在Lua中编程吗,并且我尝试在必要时使用我的一些技能,所以我尝试将玩家选择的运算符连接起来以得到结果。 / p>

我尝试在google中搜索解决方案,并且尝试打印时不以不同的方式将其串联起来,但是我只是不知道该怎么做。

uploadFile = (file, metadata) => {
    const pathToUpload = this.state.channel.id; 
    const ref = this.props.messagesRef; 
    const filePath = `chat/public/${uuidv4()}.jpg`; // uuid is a function that creates random string

    this.setState({
      uploadState: 'uploading',
      uploadTask: this.state.storageRef.child(filePath).put(file,metadata)
    },
() => {
       this.state.uploadTask.on('state_changed', snap => {
         const percentUploaded = Math.round((snap.bytesTransferred / snap.totalBytes) * 100)
         this.setState({percentUploaded})
       },
       err => {
         console.error(err)
         this.setState({
           errors: this.state.errors.concat(err),
           uploadState: 'error',
           uploadTask: null
         })
       })  
    },
    () => {
      this.state.uploadTask.snapshot.ref.getDownloadURL().then(downloadUrl => {
        console.log(downloadUrl) // get error
        this.sendFileMessage(downloadUrl, ref, pathToUpload)
      })
      .catch(err => {
        console.error(err)
        this.setState({
          errors: this.state.errors.concat(err),
          uploadState: 'error',
          uploadTask: null
        })
      })
    }
    )
  };

我希望输出结果说“ numberOne + numberTwo = ANSWER”,而不是 “ numberOne + numberOne = numberOne + numberTwo”。

1 个答案:

答案 0 :(得分:1)

您将需要某种表达引擎来执行此操作。这可能像switch语句一样简单,因为您只有4个运算符,最多不超过FLEE

前一种情况很容易编写代码:

class Program
{ 
    static void Main(string[] args) //This is a Method, named "Main". It's called when the program starts
    {
        double numberOne;
        double numberTwo;
        string method;

        Console.WriteLine("What would you like to do? <+, -, *, />");
        method = Console.ReadLine();
        Console.Write("Please type the first number: ");
        numberOne = Convert.ToDouble(Console.ReadLine());
        Console.Write("Please type the second number: ");
        numberTwo = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine(numberOne + method + numberTwo + " = " + Calculate(numberOne,numberTwo,method));
        Console.ReadKey();

    }
    static double Calculate(double input1, double input2, string operator)
    {
        switch(operator)
        {
            case "+": return input1 + input2;
            case "-": return input1 - input2;
            case "*": return input1 * input2;
            case "/": return input1 / input2;
            default: throw new InvalidOperatorinException($"Unknown operator: {operator}");
        }
    }

}