Jump to content

import UIKit

 

class ViewController: UIViewController {

 

    @IBOutlet weak var mySlider: UISlider!

    @IBOutlet weak var myLabel: UILabel!

    @IBOutlet weak var myButton: UIButton!

    

    let targetNumber = Int (arc4random_uniform(UInt32(100)))

 

    override func viewDidLoad()

    {

        super.viewDidLoad()

        myLabel.text = "Target value: \(targetNumber)"

        mySlider.minimumValue = 1

        mySlider.maximumValue = 100

 

        // Do any additional setup after loading the view, typically from a nib.

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    var score: Int = 0

    func scoreGenerator() -> (Int)

    {

        let mySliderValue = Int(mySlider.value)

        var difference = mySliderValue - targetNumber

        if  mySliderValue == targetNumber

        {

            score = 140

        }

        else if abs(difference) <= 20

        {

            score = 90

        }

        else

        {

            score = 40

        }

        return score

    }

    

    @IBAction func buttonPressed(sender: AnyObject)

    {

    

       var myAlert = UIAlertController(title: "Round Complete", message: "You landed on \(mySlider.value).  The target value was \(targetNumber).  Your score for this round is \(score).", preferredStyle: .Alert)

       

        let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

        

        myAlert.addAction(okAction)

        

        self.presentViewController(myAlert, animated: true, completion: nil)

    }

}

 

I made score a global variable and tried to initialize it by setting Int = 0, but it returns as 0 even when the if, else if, and else statements are true.  Why is it doing this?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×