swift2 计算器中的清除按钮没有响应

8ljdwjyq  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(120)

试图使我的清除按钮工作,但作为一个新的程序员,我仍然有一些逻辑查找问题。计算器是完美的工作,但当我按下清除什么都没有发生。

class ViewController: UIViewController {

enum Operation: String {
    case Divide = "/"
    case Multiply = "*"
    case Subtract = "-"
    case Add = "+"
    case Empty = "Empty"

}

@IBOutlet weak var outputLbl: UILabel!

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)
    }
}

@IBAction func numberPressed(btn: UIButton!){
   playSound()
    runningNumber += "\(btn.tag)"
    outputLbl.text = runningNumber
}

@IBAction func onDividePressed(sender: AnyObject) {
    processOperation(Operation.Divide)
}

@IBAction func onMultiplyPressed(sender: AnyObject) {
    processOperation(Operation.Multiply)
}

@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) {
    processOperation(Operation.Empty)
}

func processOperation(op: Operation) {
    playSound()

    if currentOperation != Operation.Empty{
        //run math

        //a user selected an operator, but then selected another operator without
        //first entering a number
        if runningNumber != ""{

        rightValStr = runningNumber
        runningNumber = ""

        if currentOperation == Operation.Multiply{
        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)!)"
        } else if currentOperation == Operation.Empty{

            result = ""
            outputLbl.text = result

            }

        leftValStr = result
        outputLbl.text = result

        }

        currentOperation = op

    }else{
        //first time its been pressed
        leftValStr = runningNumber
        runningNumber = ""
        currentOperation = op
    }
}

func playSound() {
    if btnSound.playing{
        btnSound.stop()
    }
    btnSound.play()
  }
 }
nzk0hqpo

nzk0hqpo1#

我不确定这一点,但尝试这个代码可能是它帮助你

@IBAction func onClearPressed(sender: AnyObject) 
{
      outputLbl.text = @"";
    //processOperation(Operation.Empty)
}
hs1rzwqc

hs1rzwqc2#

再次查看您的代码-您有一个检查:

if currentOperation != Operation.Empty { ...

然后再

... else if currentOperation == Operation.Empty {

第二个如果永远不会被处决的尸体!

ztmd8pv5

ztmd8pv53#

else{
    //first time its been pressed
    leftValStr = runningNumber
    runningNumber = ""
    currentOperation = op
    outputLbl.text = ""
}

在你的else部分你必须清除标签。首先你已经检查了操作不是空的,并且在body中定义了相同的操作。

相关问题