亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在我的 Reactjs 計算器應用程序上顯示計算器輸出

如何在我的 Reactjs 計算器應用程序上顯示計算器輸出

墨色風雨 2022-08-18 10:45:55
我正在使用一個 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你能幫忙嗎,我在這里做錯了什么?
查看完整描述

1 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

你的錯誤就在這里

this.setState({answer: (this.A+this.B).toString()}, console.log(this.state.answer))

當您嘗試顯示 this.state.answer 時,答案尚未更新。

嘗試將控制臺.log傳遞給 cb:

this.setState({answer: (this.A+this.B).toString()}, () => console.log(this.state.answer))



查看完整回答
反對 回復 2022-08-18
  • 1 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號