Swift 如何从两个文本字段中获取 Int 并生成一个随机数

Swift How To Take Int from two textfield and generate a random number

我必须在不同的网络上输入文本字段 scene.One 文本字段的数字较小,而另一个文本字段的数字较大 number.I 需要使用这些数字并使用最小数字生成随机数,然后最大的 number.Then 我需要做一个乘法问题 it.Example: 用户输入 4 并且 8.I 需要做两个大于 4 小于 [=19= 的随机数] 是我的代码:

//
//  CustomModeWithTimer.swift
//  BetaVersion0.1
//
//  Created by James Ikeler on 5/26/16.
//  Copyright © 2016 James Ikeler. All rights reserved.
//

import UIKit
import Foundation


var CustomNumber1 = UInt32(CustomInputForGame)
var CustomNumber2 = UInt32(CustomInput2ForGame)
var Random = arc4random_uniform(CustomNumber2) + CustomNumber1
var Random2 = arc4random_uniform(CustomNumber2) + CustomNumber1
var UnConvertInt = 0
var scorecustom = 0
var TimerCustom = NSTimer()
var CountDownCustom = 10
var GameOverCustom = UIAlertView()
var AnswerCustom = Int(CustomNumber1 * CustomNumber2)


class CustomModeWithTimer: UIViewController {

@IBOutlet weak var CustomInputForGame3: UITextField!
@IBOutlet weak var QuestionForCustomGame: UILabel!
@IBOutlet weak var ScoreLabelForCustom: UILabel!
@IBOutlet weak var TimerCustomLabel: UILabel!
@IBOutlet weak var RightOrWrongCustom: UILabel!

@IBOutlet weak var CustomImgForGame: UIImageView!

func IfAnswerWrong() {
    print("TEST\(AnswerCustom)")
    CustomInputForGame3.text = ""
    CustomImgForGame.image = UIImage(named:  "Label")
    CustomImgForGame.hidden = false
    RightOrWrongCustom.hidden = false
    RightOrWrongCustom.text = "Wrong!"
    RightOrWrongCustom.textColor = UIColor(red: 225, green: 0, blue: 0, alpha: 1)

}


func IfAnswerRight() {
        CountDownCustom = 10
        scorecustom += 1
        ScoreLabelForCustom.text = "Score: \(scorecustom)"
        Random = arc4random_uniform(CustomNumber2) + CustomNumber1
        Random2 = arc4random_uniform(CustomNumber2) + CustomNumber1

        QuestionForCustomGame.text = "\(Random) x \(Random2)"
        AnswerCustom = Int(Random * Random2)
        CustomImgForGame.image = UIImage(named: "Label")
        RightOrWrongCustom.hidden = false
        CustomImgForGame.hidden = false
        RightOrWrongCustom.text = "Right!"
        RightOrWrongCustom.textColor = UIColor(red: 0, green: 225, blue: 0, alpha: 1)
        CustomInputForGame3.text = ""


}

@IBAction func ConfirmAnswerCustom(sender: AnyObject) {
    TimerCustom.invalidate()
    TimerCustom = NSTimer.scheduledTimerWithTimeInterval(0.8, target: self,selector: Selector("UpdateClockCustom"), userInfo:  nil, repeats: true)
    let InputAnswerCustom = Int(CustomInputForGame3.text!)
    if InputAnswerCustom == AnswerCustom {
        IfAnswerRight();
    } else {
        IfAnswerWrong();

    }
}

func UpdateClockCustom() {
    TimerCustomLabel.text = ("Time: \(CountDownCustom--)")
    if CountDownCustom == -2 {
        TimerCustom.invalidate()
        GameOverCustom.title = "Game Over!"
        GameOverCustom.message = "Nice job you got a score of \(scorecustom).The answer to the question you missed was \(AnswerCustom)!"
        GameOverCustom.addButtonWithTitle("Play Again!")
        GameOverCustom.show()
    }
}
override func viewDidLoad() {
    super.viewDidLoad()


   // 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.
}

定义一个randBetween函数:

func randBetween(lower: Int, _ upper: Int) -> Int {
    return Int(arc4random_uniform(UInt32(upper - lower - 1))) + lower + 1
}

这 returns 一个随机数 x 其中 lower < x < upper。它不会检查 lower + 1 < upper 您可以自行决定如何处理。