【问题标题】:Make Quiz App Not Repeat Random Questions使测验应用程序不重复随机问题
【发布时间】:2015-05-11 03:41:50
【问题描述】:

我知道有一些使用 shuffle 或类似的解决方案,但我不知道是否可以在我的代码中应用它们。你能帮我么?如何让问题不再重复?

func RandomQuestions(){

    var RandomNumber = arc4random() % 28
    RandomNumber += 1

    switch(RandomNumber){
    case 1:
        QuestionLabel.text = "Who is the Numer One Best DJ According to DJ Mag 2014?"
        Button1.setTitle("Tiësto", forState: UIControlState.Normal)
        Button2.setTitle("Avicii", forState: UIControlState.Normal)
        Button3.setTitle("Hardwell", forState: UIControlState.Normal)
        Button4.setTitle("Dimitri Vegas & Like Mike", forState: UIControlState.Normal)
        CorrectAnwser = "3"
        break
    case 2:
        QuestionLabel.text = "Who is the Only DJ that played in an Olimpic Games?"
        Button1.setTitle("Avicii", forState: UIControlState.Normal)
        Button2.setTitle("Tiësto", forState: UIControlState.Normal)
        Button3.setTitle("Armin Van Buuren", forState: UIControlState.Normal)
        Button4.setTitle("Calvin Harris", forState: UIControlState.Normal)
        CorrectAnwser = "2"
        break
    }

    /* ...more questions...* /

}

【问题讨论】:

  • 您的问题到底是什么?
  • 如果有代码,这样问题就不会重复(感谢提问)@JacobsonTalom
  • 由于语法原因,我删除了我的答案 - 感谢@fogmeister 的精彩课程
  • 有人可以停止投票此页面上的所有内容吗?至少解释一下为什么?!对拒绝投票按钮感到满意绝对没有什么好处。尤其是正确答案。

标签: xcode swift random arc4random


【解决方案1】:

您的代码需要重新格式化,保持这种方式(您建议的那个),每次您需要添加一个新问题。请考虑这样做:

class Question{
 let question : String
 let optionA: String
 let optionB: String
 let optionC: String
 let optionD: String
 let correctAnswer: Int

 //add initializers here.
}

class Question{
 let question : String
 let options: [String]
 let correctAnswer: Int

 //add initializers here.
}

然后

class QuestionRepository{
 private var questions: [Question]

 /// use this to load questions to be asked only once per game, this way you will end up having the order in which you will ask questions and there will be no repetitions.
 var readyToAskQuestions : [Question] {
    let shuffledQuestions = shuffle(questions)
    return shuffledQuestions
 }      

 init()
  {
    //build your harcoded 'questions' variable here 
  }

 convenience init(url: String)
 {
   //or load your questions from a file, NSUserDefaults, database sql file, from a webservice on the internet, etc.. 
 }
} 

此处所述的随机播放功能可以是sorting arrays in swift 处的任何答案

【讨论】:

  • 非常感谢大家,我真的不明白为什么人们不喜欢这样的好作品,这个漏洞是为了获得帮助而不是判断
  • @nicolasxviii 我看到你接受的问题在这篇文章中引发了有争议的讨论,你可以随时删除接受的答案并将其分配给另一个答案。一如既往,您确信答案确实更好。
【解决方案2】:

解决此问题的最糟糕方法是不断生成随机数并“跟踪”已使用的数字。对于 100 个问题的数组,您必须平均滚动最后一个随机数 100 次才能得到最后一个可用的问题。

执行此类操作的最简单方法是在使用数组之前对其进行洗牌,然后按顺序遍历它。

您可以在这个答案中看到 Fisher-Yates shuffle 的实现...How do I shuffle an array in Swift?

那么就只是迭代数组的一种情况……

for question in shuffledArray {
    // ask question
}

你甚至可以为它创建一个生成器,让你得到next问题等等。

此外,您使用开关并打开随机值的方式是错误的。

创建一个具有属性的 Question 对象... questionText、option1、option2、correctAnswer...等...

您甚至可以创建一个选项数组并将correctAnswerIndex 存储在旁边。

然后将所有 Question 对象粘贴到一个数组中。更好的是,将它们放在 plist 文件中,以便您可以在运行时读取它们。这种方式更容易管理。

【讨论】:

    【解决方案3】:

    我的answer 到另一个帖子:

    var someArray = Array(1...28)
    
    let index = Int(arc4random_uniform(UInt32(someArray.count)))
    let randomNumber = someArray[index]
    
    someArray.removeAtIndex(index)
    

    然后你可以将randomNumber 传递给你的函数:

    func randomQuestions(question: Int) {
        switch(question) {
        case 1: // Question1
        case 2: // Question2
    }
    

    【讨论】:

    • 好的,非常感谢,找到后请告诉我@JacobsonTalom
    • 对不起,不是我。我不会轻易投反对票,除非有人粗鲁违反规则。
    • @JacobsonTalom 好的,不用担心。
    • 我实际上很惊讶他似乎还没有找到解决方案的答案,尽管这个问题似乎很容易解决(我的答案当然是最好的!:p)。
    • 好吧,他现在找到了答案……错误的答案。哈哈!哦,好吧。
    【解决方案4】:

    试图让arc4random() 不重复不是看待这个问题的正确方法。你想避免两次问同一个问题,所以你需要以某种方式跟踪你已经问过的问题。我建议使用数组,也许给每个问题某种类型的 ID,然后将每个问题 ID 存储在数组中,并在被问到时将它们删除或移动到新的“提问”数组中。

    【讨论】:

      【解决方案5】:

      我会向视图控制器添加一个变量来保存已经提出的问题:

      var askedQuestions = [Int]()
      

      然后,在您的函数中,您可以执行以下操作:

      func RandomQuestions(){
      
          var randomNumber:Int = (Int)(arc4random() % 28 + 1)
      
          while find(askedQuestions, randomNumber) != nil && askedQuestions.count < 28 {
              randomNumber = (Int)(arc4random() % 28 + 1)
          }
      
          if askedQuestions.count > 28 {
              return
          }
      
          askedQuestions.append(randomNumber)
      
          switch(randomNumber){
          case 1:
              QuestionLabel.text = "Who is the Numer One Best DJ According to DJ Mag 2014?"
              Button1.setTitle("Tiësto", forState: UIControlState.Normal)
              Button2.setTitle("Avicii", forState: UIControlState.Normal)
              Button3.setTitle("Hardwell", forState: UIControlState.Normal)
              Button4.setTitle("Dimitri Vegas & Like Mike", forState: UIControlState.Normal)
              CorrectAnwser = "3"
              break
          case 2:
              QuestionLabel.text = "Who is the Only DJ that played in an Olimpic Games?"
              Button1.setTitle("Avicii", forState: UIControlState.Normal)
              Button2.setTitle("Tiësto", forState: UIControlState.Normal)
              Button3.setTitle("Armin Van Buuren", forState: UIControlState.Normal)
              Button4.setTitle("Calvin Harris", forState: UIControlState.Normal)
              CorrectAnwser = "2"
              break
          }
      
          /* ...more questions...*/
      
      }
      

      另外请注意,当您用完所有 28 个问题时,您必须注意会发生什么。 :)

      PS:最好不要使用大写的变量名,如RandomNumber。请改用randomNumber。这是一个很好的做法。 :)

      【讨论】:

      • 最后导致无限循环
      • 你读过我写的吗?是的,一旦问了所有 28 个问题,它就不起作用了。我不知道用例是什么,以及在所有 28 个都结束后会发生什么。它重新开始吗?游戏结束了吗?不知道......不过,不知道为什么你必须对答案投反对票。 :)
      • 你在一个无限循环中运行,不仅在它完成之后,而且只要有重复的问题。只要把它修复成我的固定就可以了
      • 重复问题是什么意思? askedQuestions 数组中不能有重复的问题?我将添加一个安全检查以防止无限循环,但就像我已经说过的那样 - 我不知道作者在用完问题后要做什么。
      • 1.您不应该将 arc4random 与 mod 一起使用。 2. 你不应该为此使用开关。 3. 这是一种非常低效的查找所有随机问题的方法。 4. 对于大量问题,这将花费不确定的时间。 5. 说真的,这就像 O(undefined)。 6.任何其他答案(不仅仅是我的)都比这更好。 7.这是错误的答案,不要使用它。使用不同的答案。
      猜你喜欢
      • 2019-11-07
      • 1970-01-01
      • 2018-06-27
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多