2017-05-26 34 views
0

我正在創建一個簡單的測驗應用程序。有四種選擇,隨機生成選擇的位置。四個UILabel被設置爲顯示選擇。簡單測驗使用arc4random_uniform()

我已經寫了下面的代碼,它通常運行良好,但有時會顯示相同的選擇。

前)2017年2015年2015年1700

誤差應在for循環,但我想不通。

let questions = ["What year is now?"] 
let answers = [["2017", "2015", "1000", "1700"]] 
var rightAnswerPlacement:UInt32 = 0 
var currentQuestion = 0 

//Function that displays new question 
func newQuestion() 
{ 
    lbl.text = questions[currentQuestion]   
    rightAnswerPlacement = arc4random_uniform(4)+1   
    // Create a button 
    var button:UIButton = UIButton() 
    var x = 1   
    for i in 1...4 
    { 
     //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 
     } 
    } 
    button.setTitle(answers[currentQuestion][3], for: .normal) 
    currentQuestion += 1 
} 
+0

'currentQuestion'在哪裏得到一個值?你用它來查找'answers'中的值,但是你永遠不會給它賦值。 –

+0

對不起,我更新了我的問題 – rebecca87

+0

如果我理解正確,你想要隨機放置第一個答案(正確的一個),其他三個按順序放置?通過循環的四次迭代(btw。不要使用4作爲固定數字,但檢查answers-array的大小),您創建4個按鈕,但使用x-index-counting可以跳過其中一個答案。你還有一個額外的按鈕來回答[currentQuestion] [3] – Gerriet

回答

2

一個非常常用的方法是 「洗牌」 的答案。然後,你不必做任何if S:

let questions = [ 
    "What year is now?", 
    "What is your favorite color?" 
] 

let answers = [ 
    ["2017", "2015", "1000", "1700"], 
    ["Blue", "Red", "Yellow", "Green"] 
] 

var currentQuestion = 0 

//Function that displays new question 
func newQuestion() 
{ 
    // get the current question text 
    let thisQuestion = questions[currentQuestion] 

    // get a "shuffled" array of the current answers 
    let theseAnswers = answers[currentQuestion].shuffled() 

    lbl.text = thisQuestion 

    // loop through the shuffled answers, setting the button titles 
    for (i, answer) in theseAnswers.enumerated() { 
     if let button = view.viewWithTag(i) as? UIButton { 
      button.setTitle(answer, for: .normal) 
     } 
    } 

} 

現在,每次調用newQuestion()時間的答案會以隨機順序。


有陣洗牌的許多例子 - 這是一個很好的例子(摘自 「Nate Cook」 這裏... How do I shuffle an array in Swift?):

extension MutableCollection where Indices.Iterator.Element == Index { 
    /// Shuffles the contents of this collection. 
    mutating func shuffle() { 
     let c = count 
     guard c > 1 else { return } 

     for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) { 
      let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount))) 
      guard d != 0 else { continue } 
      let i = index(firstUnshuffled, offsetBy: d) 
      swap(&self[firstUnshuffled], &self[i]) 
     } 
    } 
} 

extension Sequence { 
    /// Returns an array with the contents of this sequence, shuffled. 
    func shuffled() -> [Iterator.Element] { 
     var result = Array(self) 
     result.shuffle() 
     return result 
    } 
} 
+0

謝謝我將盡力去做:) – rebecca87

1

更換x = 2x = x + 1。你想跳過一個答案,而不是選擇一個特定的答案。

這樣做也意味着你不需要[currentQuestion][3]的特殊情況。

+0

謝謝你的工作:) – rebecca87