我正在使用一個 reactjs 計算器應用程序。它應該像一個簡單的手持計算器一樣工作。到目前為止,我已經設法捕獲并顯示第一和第二操作數的用戶輸入以及要執行的操作(+,-,/,*)使用狀態和setState。當我按下“equals”按鈕時,麻煩就來了,它應該調用我稱之為EqualsSign()的函數,以便獲得答案并將其顯示在屏幕上。無論我輸入什么數字,我都得到0作為答案。代碼如下:EqualsSign(){ this.A=parseFloat(this.state.operand) console.log("A is ",this.A)// debuging line this.B=parseFloat(this.state.operand2) console.log("B is ", this.B) switch(this.state.operation){ case "+": console.log(this.state.operation) this.setState({answer: (this.A+this.B).toString()}, console.log(this.state.answer)) this.setState({operand: this.state.answer, operand2: '', operation: ''}) break case "-": console.log(this.state.operation) this.setState({answer: this.A+this.B}, console.log(this.state.answer)) this.setState({operand: this.state.answer, operand2: '', operation: ''}) break case "*": console.log(this.state.operation) this.setState({answer: this.A+this.B}, console.log(this.state.answer)) this.setState({operand: this.state.answer, operand2: '', operation: ''}) break case "÷": console.log(this.state.operation) this.setState({answer: this.A+this.B}, console.log(this.state.answer)) this.setState({operand: this.state.answer, operand2: '', operation: ''}) break default: // }}計算應用和控制臺日志的屏幕截圖,顯示 0 作為 A+B 的輸出(A=15,B=12你能幫忙嗎,我在這里做錯了什么?
如何在我的 Reactjs 計算器應用程序上顯示計算器輸出
墨色風雨
2022-08-18 10:45:55