Swift 4 - 测验应用随机问题

时间:2018-01-18 19:07:17

标签: ios swift xcode random var

我是Swift的初学者,通常是Xcode。提前感谢您的时间和帮助:)

我正在做一个测验应用程序,我尝试随机提问...我有一个函数来生成我试图在viewDidLoad()中应用的随机数...不幸的是我无法猜测如何使用该信息在他的变量" var currentQuestion = 0"

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    generateRandomNumber(0, 9, 10)
}

override func viewDidAppear(_ animated: Bool) {
    newQuestion()
}


// Quiz//

let questions = ["Q1","Q2","Q3","Q4","Q5"]

let answers = [["A","2","3"],["B","2","3"],["C","2","3"],["D","2","3"],["E","2","3"]]


//Variables
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0


//Generate Random Numbers
func generateRandomNumber(_ from:Int, _ to:Int, _ qut:Int?) -> [Int] {
    var myRandomNumbers = [Int]() //All our generated numbers
    var numberOfNumbers = qut //How many numbers to generate

    let lower = UInt32(from) //Generate from this number..
    let higher = UInt32(to+1) //To this one

    if numberOfNumbers == nil || numberOfNumbers! > (to-from) + 1 {
        numberOfNumbers = (to-from) + 1
    }

    while myRandomNumbers.count != numberOfNumbers {
        let myNumber = arc4random_uniform(higher - lower) + lower

        if !myRandomNumbers.contains(Int(myNumber)) {
            myRandomNumbers.append(Int(myNumber))
        }
    }
    return myRandomNumbers
}


//Label
@IBOutlet weak var lbl: UILabel!


//Button
@IBAction func action(_ sender: AnyObject) {
    if (sender.tag == Int(rightAnswerPlacement)) {
        print ("RIGHT!")
    } else {
        print ("WRONG!!!!!!")
    }

    if (currentQuestion != questions.count) {
        newQuestion()
    } else {
    }
}

//Function that displays new question
func newQuestion() {
    lbl.text = questions[currentQuestion]

    rightAnswerPlacement = arc4random_uniform(3)+1

    //Create a button
    var button:UIButton = UIButton()

    var x = 1

    for i in 1...3 {
        //Create a button
        button = view.viewWithTag(i) as! UIButton

        if (i == Int(rightAnswerPlacement)) {
            button.setTitle(answers[currentQuestion][0], for: .normal)
        } else {
            button.setTitle(answers[currentQuestion][x], for: .normal)
            x = 2
        }
    }
    currentQuestion += 1
}

知道怎么可能解决?

1 个答案:

答案 0 :(得分:0)

首先,您需要存储您生成的随机问题数组(注意,您似乎试图生成10个随机数,但只有5个问题)。

然后直接使用它来访问数组中的问题而不是直接使用currentQuestion。

所以改成它:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    questionArray = generateRandomNumber(0, 9, 10)
}

override func viewDidAppear(_ animated: Bool) {
    newQuestion()
}


// Quiz//

let questions = ["Q1","Q2","Q3","Q4","Q5"]

let answers = [["A","2","3"],["B","2","3"],["C","2","3"],["D","2","3"],["E","2","3"]]


//Variables
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0
var questionArray: [Int] = [] // Just an initial value


//Generate Random Numbers
func generateRandomNumber(_ from:Int, _ to:Int, _ qut:Int?) -> [Int] {
    var myRandomNumbers = [Int]() //All our generated numbers
    var numberOfNumbers = qut //How many numbers to generate

    let lower = UInt32(from) //Generate from this number..
    let higher = UInt32(to+1) //To this one

    if numberOfNumbers == nil || numberOfNumbers! > (to-from) + 1 {
        numberOfNumbers = (to-from) + 1
    }

    while myRandomNumbers.count != numberOfNumbers {
        let myNumber = arc4random_uniform(higher - lower) + lower

        if !myRandomNumbers.contains(Int(myNumber)) {
            myRandomNumbers.append(Int(myNumber))
        }
    }
    return myRandomNumbers
}


//Label
@IBOutlet weak var lbl: UILabel!


//Button
@IBAction func action(_ sender: AnyObject) {
    if (sender.tag == Int(rightAnswerPlacement)) {
        print ("RIGHT!")
    } else {
        print ("WRONG!!!!!!")
    }

    if (currentQuestion != questions.count) {
        newQuestion()
    } else {
    }
}

//Function that displays new question
func newQuestion() {
    lbl.text = questions[questionArray[currentQuestion]]

    rightAnswerPlacement = arc4random_uniform(3)+1

    //Create a button
    var button:UIButton = UIButton()

    var x = 1

    for i in 1...3 {
        //Create a button
        button = view.viewWithTag(i) as! UIButton

        if (i == Int(rightAnswerPlacement)) {
            button.setTitle(answers[questionArray[currentQuestion]][0], for: .normal)
        } else {
            button.setTitle(answers[questionArray[currentQuestion]][x], for: .normal)
            x = 2
        }
    }
    currentQuestion += 1
}

(我认为代码是正确的,但我目前正在Xcode中运行一些测试,因此无法检查。如果有问题请发表评论。)