如何确保测验应用程序中显示正确的答案选择?

机长代码

我正在开发一个测验应用程序,在其中我有各种各样的问题和答案。答案选择通过与相应问题具有相同的索引与问题相关联(例如,答案数组中的第一个答案选择与问题数组的第一个索引一起使用,依此类推)。但是,我正在从问题数组中随机选择所要提问的问题,因此相应的答案不会随所显示的正确问题一起出现。随机选择和显示的问题中出现正确答案的最佳方法是什么?另外,如何防止已经回答的问题在测验失败并被重置之前不会再次被随机选择?谢谢。

这是我的代码:

import UIKit 

class ViewController:UIViewController {
    //random question generation function
    func randomQuestion() {
        index = Int(arc4random_uniform(UInt32(questions.count)))
        questionLabel.text = questions[index]
    }

    //costants
    let questions = ["Who is Thor's half brother?", "What is the name of Thor's hammer?"]
    var answers = [["Atum", "Loki", "Red Norvell", "Kevin Masterson"], ["Mjolinr", "Uru", "Stormbreaker", "Odin's Staff"]]

    //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)) {
            print ("Correct!")
        } else {
            wrongSeg()
            print ("Wrong!")
        }

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

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

    //function that displays new question
    func newQuestion()
    {
        //countdown timer section
        randomQuestion()

        rightAnswerBox = arc4random_uniform(4)+1

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

        var x = 1

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

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

        currentQuestion += 1
        randomImage()
    }
ord

我建议使用更合适的数据结构。

就像是:

class Question {
    var id: Int
    var questionText: String
    var answers: [String]
    var answered = false
}

然后,您可以将Question数组随机化。您可以跟踪所使用的索引,并在数组中向前移动直到结束。

在循环结束时,检查尚未回答的问题,删除已回答的问题(或创建一个新数组),然后重复该过程。

您可以通过数组枚举

var questions = [Question]()

//populate the array

    for (index, question) in questions.enumerated() {
        //This returns the question as well as the current index
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在测验应用程序android的结果中显示正确答案

具有多个正确答案的多项选择测验应用程序的设计方法

在测试应用程序中提交多个答案后,Angular 计算正确答案并显示在结果中

答案正确时,Android测验应用程序不正确

如何让测验显示学生答案并显示正确答案和成绩?

Android测验,如何显示答案是否正确并随机分配答案

如何在Angular测验应用程序中显示选项1的说明?

测验,要在选择的答案不正确时同时显示正确答案和错误答案

多种选择的测验应用程序

iOS中的测验应用程序控件,具有不同数量的问题答案

我的测验应用程序未显示测验选项

如何在测验应用程序中创建后退按钮

如何在Google测验中添加具有正确答案的多项选择题

如何突出显示在线测验的正确和错误答案?

如何确保我的应用程序已正确配置?

在 javascript 测验中显示正确答案时遇到问题

我如何映射问题以纠正此 Python 测验程序中的多项选择答案?

在Android中显示正确的应用程序

测验应用程序中的进度栏

选择数组中的正确答案后如何显示消息?

测验:如何集成在 UITextField 中声明正确的替代/类似答案

如何在测验中为checkboxGridItem问题分配正确答案?

如何在VueJS中绑定多项选择测验答案

选择按钮时如何检查测验中的答案

如何在 /bootstrap 4.6 应用程序中正确显示日期时间选择器?

我正在制作一个 PHP 测验应用程序。如何确保问题只问一次?

在测验应用程序中从 Firestore 中检索好的答案以避免用户在回答问题之前知道好的答案的最佳方法是什么

显示测验应用程序的结果 (Android Studio)

测验应用程序在Android Studio上随机化答案