计算并显示为标签

时间:2018-12-21 01:27:55

标签: ios swift

我想计算每次运行次数,并显示在运行率标签上。

正如您在我的代码中看到的那样,运行速度将随着我使用步进器进行运行而变化。

运行速率最多需要显示2个小数点(例如4.50或10.74等)

我想创建函数,但是我不知道如何计算。

我该怎么做?

// RUNS

var runsInt = 0
var oversFloat: Float = 0.0
var runRate: Double = 0.0

@IBOutlet weak var runsLabel: UILabel!
@IBOutlet weak var displayRunsLabel: UILabel!
@IBOutlet weak var oversLabel: UILabel!
@IBOutlet weak var oversRectangle: UILabel!
@IBOutlet weak var displayOversLabel: UILabel!
@IBOutlet weak var runRateLabel: UILabel!
@IBOutlet weak var displayRunRateLabel: UILabel!

@IBAction func RunStepper(_ sender: UIStepper) {
let runsValue = Int(sender.value)
displayRunsLabel.text = String(runsValue)
}

// OVERS
 @IBAction func OversStepper(_ sender: UIStepper) {
    let value = Int(sender.value)
    let remainder = value % 10
    if remainder == 6 {
        sender.value = Double(value + 4)
    } else if remainder == 9 {
        sender.value = Double(value - 4)
    }
    displayOversLabel.text = String(format: "%.1f", sender.value / 10)   
}

1 个答案:

答案 0 :(得分:1)

我使用this page获取有关计算运行率的信息。

runRate = (total runs scored)/(total overs faced)

注意:如果overs47.5,则步进值将为475.0,并且 oversInt将设置为475。计算runRate时,47.5 实际上代表47 5/6,我们可以计算为 Double(oversInt / 10) + Double(oversInt % 10) / 6

创建一个名为calculateRunRate的函数,然后 将属性观察者(didSet)添加到runsIntoversInt以触发runRate的计算。然后,只要这些值中的任何一个发生更改,runRate都会被重新计算,displayRunRate标签也会被更新。

// RUNS

var runsInt = 0 {
    didSet {
        calculateRunRate()
    }
}

@IBOutlet weak var runsLabel: UILabel!

@IBOutlet weak var displayRunsLabel: UILabel!

@IBAction func RunStepper(_ sender: UIStepper) {
    runsInt = Int(sender.value)
    displayRunsLabel.text = String(runsInt)
}


// OVERS

var oversInt = 0 {
    didSet {
        calculateRunRate()
    }
}

@IBOutlet weak var oversLabel: UILabel!
@IBOutlet weak var oversRectangle: UILabel!

@IBOutlet weak var displayOversLabel: UILabel!

@IBAction func OversStepper(_ sender: UIStepper) {
    var value = Int(sender.value)
    let remainder = value % 10
    if remainder == 6 {
        value += 4
    } else if remainder == 9 {
        value -= 4
    }
    sender.value = Double(value)
    oversInt = value

    displayOversLabel.text = String(format: "%.1f", sender.value / 10)
}


// RUN RATE

var runRate = 0.0
let maxOvers = 40.0

@IBOutlet weak var runRateLabel: UILabel!
@IBOutlet weak var displayRunRateLabel: UILabel!
@IBOutlet weak var displayRequiredRunRateLabel: UILabel!

func calculateRunRate() {
    var oversDouble = 0.0

    // Don't divide by zero
    if oversInt == 0 {
        // can this even happen?
        // set runRate to some value that makes sense
        runRate = 0.0
    } else {
        oversDouble = Double(oversInt / 10) + Double(oversInt % 10) / 6
        runRate = Double(runsInt) / oversDouble
    }
    displayRunRateLabel.text = String(format: "%.2f", runRate)

    // required run rate

    let targetScore = Int(targetScoreLabel.text ?? "") ?? 0

    var requiredRunRate = 0.0

    if oversDouble < maxOvers {
        requiredRunRate = Double(targetScore - runsInt) / (maxOvers - oversDouble)
    }

    displayRequiredRunRateLabel.text = String(format: "%.2f", requiredRunRate)
}