无法在Swift中使计算器应用程序获得多个数字?

时间:2017-11-10 10:30:15

标签: swift append calculator

所以这是我的操作课程:

class Calculation {

var currentNumber: String = ""
var resultNumber = Int()
var operationInput = String()


func operationIdentifier() {

    if operationInput == "=" {
        resultNumber = Int(currentNumber)!
        print("\(resultNumber)")
    } else if operationInput == "+" {
        resultNumber += Int(currentNumber)!
        print("\(resultNumber)")
    } else if operationInput == "-" {
        resultNumber -= Int(currentNumber)!
        print("\(resultNumber)")
    } else if operationInput == "*" {
        resultNumber *= Int(currentNumber)!
        print("\(resultNumber)")
    } else if operationInput == "/" {
        resultNumber /= Int(currentNumber)!
        print("\(resultNumber)")
    } else {
        print("Operation does not exist.")
    }

    print("\(resultNumber)")
}

这就是我得到这个按钮来获得数字标题的地方:

@IBAction func numberPressed(_ sender: UIButton) {

    calculation.currentNumber = (sender.titleLabel?.text!)!
    calculation.currentNumber.append(<#T##other: String##String#>)
    //calculation.currentNumber.append((sender.titleLabel?.text)!) This is how I use it to append.
    calculation.operationIdentifier()
    resultTextField.text = "\(calculation.currentNumber)"

}

append命令用于代码部分中显示的字符串,我用它来添加到前一个数字的末尾,结果总是一些Int的两倍,例如:如果按5,则返回55。 / p>

2 个答案:

答案 0 :(得分:1)

首先,你在这里覆盖了价值:calculation.currentNumber = (sender.titleLabel?.text!)!。其次,按下后需要清除当前号码。

这是我的概念(您编辑的代码),您可以在Playground中尝试。

enum Operator: String {
    case plus = "+"
    case minus = "-"
    case multiply = "*"
    case divide = "/"
    case equal = "="

}

class Calculation {

    var currentNumber: String = ""
    var resultNumber = Int()
    var operationInput = String()

    func recalculate() {
        if resultNumber == 0 {
            resultNumber = Int(currentNumber) ?? 0
            return
        }

        guard let sign = Operator(rawValue: operationInput), let number = Int(currentNumber) else { return }

        switch sign {
            case .plus: resultNumber += number
            case .minus: resultNumber -= number
            case .divide: resultNumber /= number
            case .multiply: resultNumber *= number
            case .equal: resultNumber = number
        }
    }

    func operatorPressed(_ op: Operator) {
        recalculate()
        operationInput = op.rawValue
        currentNumber = ""

        print(resultNumber)
    }

    func numberPressed(_ number: String) {
        currentNumber.append(number)
    }
}

let c = Calculation()
c.numberPressed("5")
c.numberPressed("5")
c.operatorPressed(.plus)
c.numberPressed("5")
c.operatorPressed(.multiply)
c.numberPressed("5")
c.operatorPressed(.equal)
c.operatorPressed(.plus)
c.numberPressed("5")
c.operatorPressed(.equal)

答案 1 :(得分:0)

numberPressed中,您要从标签文本中设置currentNumber,然后再向其添加相同的值。它应该是这样的:

@IBAction func numberPressed(_ sender: UIButton) {

    guard let label = sender.titleLabel, let numberString = label.text else{
        return
    }

    calculation.currentNumber.append(numberString)

    print("currentNumber = \(calculation.currentNumber)")

}

另外,为什么要在calculation.operationIdentifier()中致电numberPressed?不会用operatorPressed方法吗?