首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何像我的价值观一样获得随机化的钥匙?

如何像我的价值观一样获得随机化的钥匙?
EN

Stack Overflow用户
提问于 2017-07-22 00:40:12
回答 1查看 57关注 0票数 0

我正在为一个iOS问答应用程序编写代码,到目前为止,这段代码包含了一个字典,它将问题作为关键,并根据问题的价值来回答这个问题。我的代码将一个键(问题)数组随机化,并选择一个要随机显示的键。我还将值(答案)放在一个值数组中,并将它们随机化,并确保选择了问题数组的任何索引,并选择了相同的答案数组索引。

但是,当我运行模拟器时,问题标签文本保持不变,没有从要选择的问题数组中选择一个新的索引作为新的显示问题,但另一方面,在我选择答案之后,要更改的答案数组。

简单地说,问题标签文本始终保持不变,按钮的标题(值数组)确实选择了不同的索引。,我如何才能到达问题标签更改而不保持不变的地方?

这是我的代码:

代码语言:javascript
复制
class QuestionDetails {
    let QADictionary = [
        "Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],
        "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"],
        "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]
    ]

    //get question list
    func questionWithAnswers() {

        let listQuestions = QuestionDetails()

        var questionList = Array(listQuestions.QADictionary.keys)

        //random question index
        var rand = Int(arc4random_uniform(UInt32(questionList.count)))

        let randAnswers = rand

        //random answer index
        var answerList = Array(listQuestions.QADictionary.values)

        var choices = answerList[randAnswers]

        //fetch questions from list
        var question = questionList[rand]

        questionLabel.text = question

        //function for new question and button titles
        rightAnswerBox = arc4random_uniform(4)+1

        //create button
        var button:UIButton = UIButton()
        var x = 1

        for index in 1...4
        {
            button = view.viewWithTag(index) as! UIButton

            if (index == Int(rightAnswerBox))
            {
                button.setTitle(choices[0], for: .normal)
            }
            else {
                button.setTitle(choices[x], for: .normal)
                x += 1
            }

            randomImage()
        }
    }

    //variables
    var currentQuestion = 0
    var rightAnswerBox:UInt32 = 0
    var index = 0

    //Question Label
    @IBOutlet weak var questionLabel: UILabel!

    //Answer Button
    @IBAction func buttonAction(_ sender: AnyObject) {

        if (sender.tag == Int(rightAnswerBox)) {

            //removes asked question

            print ("Correct!")
        }
        else {
            wrongSeg()
            print ("Wrong!")
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        questionWithAnswers()
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-07-22 01:12:12

以字典的keys为例。强迫列队。以数组的count为例。在0和count之间生成一个随机数,这是不包含的.将随机数降到Int。获取该索引处的数组元素。把它当作你的钥匙。

对上述方法的检验:

代码语言:javascript
复制
extension ClosedRange where Bound : FloatingPoint {
    public func random() -> Bound {
        let range = self.upperBound - self.lowerBound
        let randomValue = (Bound(arc4random_uniform(UINT32_MAX)) / Bound(UINT32_MAX)) * range + self.lowerBound
        return randomValue
    }
}

let QADictionary = [
    "Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],
    "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"],
    "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]
]
func test () {
    let questions = Array(QADictionary.keys)
    let r = (0...Double(questions.count)).random()
    let randomQuestion = questions[Int(r)]
    print(randomQuestion)
}
(1...40).forEach {_ in test()}
/*
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is the father of Thor?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is the father of Thor?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?

*/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45249499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档