计算器应用程序不会始终将 multiple-digit 数字和运算 return 归零?
Calculator app does not get multiple-digit numbers and operations return zero at all times?
我用的是 Swift 4,数字都是 Int。
所以我做了一个计算class file:
import Foundation
import UIKit
class Calculation {
var result = Int()
var operationButton = String()
var currentNumber = Int()
func calculator() {
switch operationButton {
case "+":
result = result + currentNumber
case "-":
result = result - currentNumber
case "*":
result = result * currentNumber
case "/":
result = result / currentNumber
case "=":
result = currentNumber
default:
break
}
}
func textClear() {
result = 0
currentNumber = 0
operationButton = "="
}
然后我在 UIControllerView 中为 AC 按钮、数字按钮和操作按钮制作了 3 个函数,以直接从按钮标题中获取“0-9”和“-,+,*,/,=”。
class ViewController: UIViewController {
@IBOutlet weak var resultTextField: UITextField!
var calculation = Calculation()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
calculation.operationButton = "="
resultTextField.text = "\(calculation.result)"
}
@IBAction func allClearButtonPressed(_ sender: UIButton) {
calculation.textClear()
resultTextField.text = "\(calculation.result)"
}
@IBAction func operationButtonPressed(_ sender: UIButton) {
calculation.operationButton = sender.titleLabel!.text as String!
calculation.operationButton = sender.titleLabel!.text!
calculation.calculator()
resultTextField.text = "\(calculation.result)"
}
@IBAction func numberButtonPressed(_ sender: UIButton) {
calculation.currentNumber = Int((sender.titleLabel?.text)!)!
let numberInput = String(calculation.currentNumber)
resultTextField.text = numberInput
}
}
第一个问题是,每当我尝试输入一个数字(例如 21)时,它只是 returns 2,然后被 1 替换。第二个问题是运算符总是 return 零"resultTextField".
您的 numberButtonPressed
函数读取按钮的文本,替换 currentNumber
,然后将其分配给 resultTextField.text
。您想要的是将新数字添加到当前号码的功能。在你的 Calculator
class.
里面
func pressedDigit(_ digit: Int) {
currentNumber = currentNumber * 10 + digit
}
然后您可以调用此函数并将currentNumber分配给您的resultTextField
。
@IBAction func numberButtonPressed(_ sender: UIButton) {
guard let digit = sender.titleLabel?.text?.flatMap({ Int(String([=11=])) }) else { return }
calculation.pressedDigit(value: digit)
resultTextField.text = "\(calculation.currentNumber)"
}
您还必须在每次计算后重置 currentNumber
。
我用的是 Swift 4,数字都是 Int。
所以我做了一个计算class file:
import Foundation
import UIKit
class Calculation {
var result = Int()
var operationButton = String()
var currentNumber = Int()
func calculator() {
switch operationButton {
case "+":
result = result + currentNumber
case "-":
result = result - currentNumber
case "*":
result = result * currentNumber
case "/":
result = result / currentNumber
case "=":
result = currentNumber
default:
break
}
}
func textClear() {
result = 0
currentNumber = 0
operationButton = "="
}
然后我在 UIControllerView 中为 AC 按钮、数字按钮和操作按钮制作了 3 个函数,以直接从按钮标题中获取“0-9”和“-,+,*,/,=”。
class ViewController: UIViewController {
@IBOutlet weak var resultTextField: UITextField!
var calculation = Calculation()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
calculation.operationButton = "="
resultTextField.text = "\(calculation.result)"
}
@IBAction func allClearButtonPressed(_ sender: UIButton) {
calculation.textClear()
resultTextField.text = "\(calculation.result)"
}
@IBAction func operationButtonPressed(_ sender: UIButton) {
calculation.operationButton = sender.titleLabel!.text as String!
calculation.operationButton = sender.titleLabel!.text!
calculation.calculator()
resultTextField.text = "\(calculation.result)"
}
@IBAction func numberButtonPressed(_ sender: UIButton) {
calculation.currentNumber = Int((sender.titleLabel?.text)!)!
let numberInput = String(calculation.currentNumber)
resultTextField.text = numberInput
}
}
第一个问题是,每当我尝试输入一个数字(例如 21)时,它只是 returns 2,然后被 1 替换。第二个问题是运算符总是 return 零"resultTextField".
您的 numberButtonPressed
函数读取按钮的文本,替换 currentNumber
,然后将其分配给 resultTextField.text
。您想要的是将新数字添加到当前号码的功能。在你的 Calculator
class.
func pressedDigit(_ digit: Int) {
currentNumber = currentNumber * 10 + digit
}
然后您可以调用此函数并将currentNumber分配给您的resultTextField
。
@IBAction func numberButtonPressed(_ sender: UIButton) {
guard let digit = sender.titleLabel?.text?.flatMap({ Int(String([=11=])) }) else { return }
calculation.pressedDigit(value: digit)
resultTextField.text = "\(calculation.currentNumber)"
}
您还必须在每次计算后重置 currentNumber
。