[英]Swift Calculator operator issue
大家好,謝謝您訪問我的頁面。 我目前正在學習為班級創建一個計算器,但是請注意,例如,當我鍵入內容時發生錯誤:
5 + 5 = 10 //這是正確的
5 + 2 * 3 + 5 = 26 //尚無問題
5 + 2 = 7 +(將最后一個運算符與數字相加。這意味着在我選擇要添加的數字之前,屏幕將顯示9。)
我應該如何思考才能解決此問題的任何建議將不勝感激。
import UIKit
import AVFoundation
class ViewController: UIViewController {
enum Operation: String {
case Divide = "/"
case Multiple = "*"
case Subtract = "-"
case Add = "+"
case Empty = "Empty"
}
@IBOutlet weak var outputLbl: UILabel!
// This is initialized once the app loads.
var btnSound: AVAudioPlayer!
var runningNumber = ""
var leftValStr = ""
var rightValStr = ""
var currentOperation: Operation = Operation.Empty
var result = ""
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("btn", ofType: "wav")
let soundUrl = NSURL(fileURLWithPath: path!)
do {
try btnSound = AVAudioPlayer(contentsOfURL: soundUrl)
btnSound.prepareToPlay()
} catch let err as NSError {
print(err.debugDescription)
}
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func numberPressed(btn: UIButton) {
playSound()
// Each button is assigned to a tag from 0-9.
// When button is pressed it is stored to btn.
runningNumber += "\(btn.tag)"
outputLbl.text = runningNumber
}
@IBAction func onDividePressed(sender: AnyObject) {
processOperation(Operation.Divide)
}
@IBAction func onMultiplyPressed(sender: AnyObject) {
processOperation(Operation.Multiple)
}
@IBAction func onSubtractPressed(sender: AnyObject) {
processOperation(Operation.Subtract)
}
@IBAction func onAddPressed(sender: AnyObject) {
processOperation(Operation.Add)
}
@IBAction func onEqualPressed(sender: AnyObject) {
processOperation(currentOperation)
}
@IBAction func onClearPressed(sender: AnyObject) {
clearCalcuator()
}
// op is what is storing the Operator. It could have been named anything
func processOperation(op: Operation) {
playSound()
if currentOperation != Operation.Empty {
// Run some math.
// A user selected an operator, but then selected another
// operator without first selecting a number
if runningNumber != "" {
rightValStr = runningNumber
runningNumber = ""
}
if currentOperation == Operation.Multiple {
result = "\(Double(leftValStr)! * Double(rightValStr)!)"
} else if currentOperation == Operation.Divide {
result = "\(Double(leftValStr)! / Double(rightValStr)!)"
} else if currentOperation == Operation.Subtract {
result = "\(Double(leftValStr)! - Double(rightValStr)!)"
} else if currentOperation == Operation.Add {
result = "\(Double(leftValStr)! + Double(rightValStr)!)"
}
leftValStr = result
outputLbl.text = result
currentOperation = op
} else {
// This is the first time an Operator has been pressed.
leftValStr = runningNumber
runningNumber = ""
currentOperation = op
}
}
func clearCalcuator() {
playSound()
if outputLbl.text != "0" {
runningNumber = ""
leftValStr = ""
rightValStr = ""
currentOperation = Operation.Empty
}
outputLbl.text = "0"
}
// Function was created to keep the code DRY.
// Now can use playSound() on top. Exp: Line 51
func playSound() {
if btnSound.playing {
btnSound.stop()
}
btnSound.play()
}
}
我仍然是SWIFT的初學者,但希望通過向了解得更多的人學習來改善自己。 如果您對我目前的代碼也有任何建議,以及如何改進,我們將很高興歡迎所有建議。 再次感謝你。
您描述的行為是以下事實的結果: =
的@IBAction
傳遞了currentOperation
的Operation
,這意味着如果最后一個操作為+
,則意味着=
按鈕的行為將與再次單擊+
。 這就是為什么您看到自己的行為的原因。
為了解決這個問題,您需要確保在按=
, processOperation
會將currentOperation
設置為其他值,以便在您下次調用processOperation
,不執行進一步的計算。 為此,您需要做的就是在=
Operation
增加一種case
:
enum Operation: String {
case Divide = "/"
case Multiple = "*"
case Subtract = "-"
case Add = "+"
case Equal = "="
case Empty = "Empty"
}
然后將@IBAction
更改為=
以僅通過此Operation
而不是currentOperation
:
@IBAction func onEqualPressed(sender: AnyObject) {
processOperation(.Equal)
}
因此,當您調用processOperation
,它將執行currentOperation
,然后完成時,它將currentOperation
重置為Operation.Equal
,下次您下次對processOperation
進行下一次調用時,它將不會基於重復任何進一步的計算.Equal
。
您應該將其保存到一個變量中,除非清除該變量,否則將對其進行下一步操作。
var answer = 5 + 2
在計算器中顯示您的answer
變量,然后如果按下另一個操作按鈕,則使用答案變量ex + 9將是answer + 9
,如果用戶點擊clear
,則單擊=
將更新ui以顯示16
,只需將answer = 0
設置answer = 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.