一系列问题迅速

时间:2015-03-11 04:42:42

标签: ios swift

我在swift中创建一个测验应用,同时使用来自tut的示例代码。但一系列问题都无法奏效。问题弹出,但按顺序。他们没有随机出现。代码似乎很好,我不知道这是在iPhone模拟器中的故障或什么。这是我的代码。

import Foundation


class QuestionModel {

var questions: Array<Question>

init () {

    questions = []
    questions.append(Question(question: "What year did WWII start?", answers:["1939", "1940", "1941", "1942"], correctAnswerIndex: 0))
    questions.append(Question(question: "What day was D-Day?", answers:["June 6, 1944", "June 16, 1944", "June 26, 1944", "June 16, 1943"], correctAnswerIndex: 0))
    questions.append(Question(question: "What country was first invaded by Germany?", answers:["France", "Belgium", "Poland", "Russia"], correctAnswerIndex: 2))
    questions.append(Question(question:"Which article of the Weimar Constitution granted Hitler emergency powers essentially allowing him to avoid parliament? ", answers:["Article 26", "Article 86", "Article 3", "Article 48"], correctAnswerIndex: 3))
    questions.append(Question(question:"Who was the leader of the Soviet Union during World War II?", answers:["Lenin", "Trotsky", "Stalin",  "Khruschev"], correctAnswerIndex: 2))
    questions.append(Question(question:"The main Axis powers of WWII Consisted of: Germany, _____, _____", answers:["Italy, Japan", "Russia, Japan", "Romania, Russia", "Japan, Romania"], correctAnswerIndex: 0))

}



}

class Question {
var question: String
var answers: Array<String>
var correctAnswerIndex: Int

init(question: String,answers:Array<String>,correctAnswerIndex: Int) {
    self.question = question
    self.answers = answers
    self.correctAnswerIndex = correctAnswerIndex
}

func isGuessCorrect(guessNumber: Int) -> Bool {

    return correctAnswerIndex == Int(guessNumber-1)

}

}

1 个答案:

答案 0 :(得分:4)

Output result of following code

import UIKit

class QuestionModel {
    var questions: Array<Question>

    init () {
        questions = []
        questions.append(Question(question: "What year did WWII start?", answers:["1939", "1940", "1941", "1942"], correctAnswerIndex: 0))
        questions.append(Question(question: "What day was D-Day?", answers:["June 6, 1944", "June 16, 1944", "June 26, 1944", "June 16, 1943"], correctAnswerIndex: 0))
        questions.append(Question(question: "What country was first invaded by Germany?", answers:["France", "Belgium", "Poland", "Russia"], correctAnswerIndex: 2))
        questions.append(Question(question:"Which article of the Weimar Constitution granted Hitler emergency powers essentially allowing him to avoid parliament? ", answers:["Article 26", "Article 86", "Article 3", "Article 48"], correctAnswerIndex: 3))
        questions.append(Question(question:"Who was the leader of the Soviet Union during World War II?", answers:["Lenin", "Trotsky", "Stalin",  "Khruschev"], correctAnswerIndex: 2))
        questions.append(Question(question:"The main Axis powers of WWII Consisted of: Germany, _____, _____", answers:["Italy, Japan", "Russia, Japan", "Romania, Russia", "Japan, Romania"], correctAnswerIndex: 0))
    }
}

class Question {
    var question: String
    var answers: Array<String>
    var correctAnswerIndex: Int

    init(question: String,answers:Array<String>,correctAnswerIndex: Int) {
        self.question = question
        self.answers = answers
        self.correctAnswerIndex = correctAnswerIndex
    }

    func isGuessCorrect(guessNumber: Int) -> Bool {
       return correctAnswerIndex == Int(guessNumber-1)
    }

    var description:String {
        var str = "question = \(self.question)"
            for answer in answers {
                str += " " + answer
            }
        return str + " \(self.correctAnswerIndex)"
    }
 }

 var questionModel = QuestionModel()

 func randomQuestion(# queModel:QuestionModel){
     var questions = queModel.questions
     var randomIndex = Int(arc4random_uniform(UInt32(questions.count)))
     println(questions[randomIndex].description)
 }

 for _ in 1...5 {
     randomQuestion(queModel: questionModel)
 } 

我在Question类中添加了一个描述方法。可能对你有帮助。