【问题标题】:Kotlin: Shuffle answers in multiple choice quizKotlin:在多项选择测验中随机播放答案
【发布时间】:2020-10-03 13:53:44
【问题描述】:

我在 Kotlin 中创建了一个多项选择测验。我想打乱每个问题中的答案,并确定正确答案的新位置,以检查用户提交的答案。我创建了一个随机播放的函数,但不确定如何正确实现它。任何帮助将不胜感激。

有问题的对象

object ConstantsAnalysis {
        const val TOTAL_CORRECT: String = "total_correct"
        const val TOTAL_OPP: String = "total_opp"
        fun getQuestions3(): ArrayList<Questions3> {
            val questionList = ArrayList<Questions3>()
            val q1 = Questions3(1, null, "On a graph, the horizontal line along which data are plotted is the _____",
                "y axis", "x axis", "origin", "quadrant", 2, R.string.Jones_1997, null)

            val q2 = Questions3(2, null, "On a graph, the vertical line along which data are plotted is the _____",
                "y axis", "origin", "x axis", "quadrant", 1, R.string.Jones_1997, R.string.Holmes_1977)

            questionList.addAll(listOf(q1, q2))
            questionList.shuffle()
            return questionList
        }
    }

数据类

data class Questions3(val id: Int, val image: Int?, val question: String, val option1: String, val option2: String, val option3: String, val option4: String, val correctAnswer: Int, val dialogBox: Int?, val dialogBox2: Int?)

问题活动

class QuestionsActivityAnalysis : AppCompatActivity(), View.OnClickListener {

    private var mCurrentPosition:Int = 1
    private var mQuestionsList:ArrayList<Questions3>? = null
    private var mSelectedOptionPosition:Int = 0
    private var mCorrectAnswers: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_questions_analysis)
        val actionBar = supportActionBar
        actionBar!!.title = "Assessment"
        actionBar.setDisplayHomeAsUpEnabled(true)
        actionBar.setDisplayShowHomeEnabled(true)
        mQuestionsList = getQuestions3()
        setQuestion()
        radio_button1.setOnClickListener(this)
        radio_button2.setOnClickListener(this)
        radio_button3.setOnClickListener(this)
        radio_button4.setOnClickListener(this)
        btn_submit.setOnClickListener(this)
    }
fun shuffleOptions(questions3: List<Questions3>): List<Questions3> {
        val result: MutableList<Questions3> = mutableListOf()
        for (q in questions3) {
            val options = listOf(q.option1, q.option2, q.option3, q.option4)
            val shuffledOptions = options.shuffled()
            val newCorrectAnswer = shuffledOptions.indexOf(options[q.correctAnswer])
            result.add(Questions3(q.id, q.image, q.question, shuffledOptions[0], shuffledOptions[1], shuffledOptions[2], shuffledOptions[3], newCorrectAnswer, q.dialogBox, q.dialogBox2))
        }
        return result
    }

private fun defaultOptionsView(){
    val options = ArrayList<TextView>()
    options.add(0, radio_button1)
    options.add(1, radio_button2)
    options.add(2, radio_button3)
    options.add(3, radio_button4)

}
override fun onClick(v: View?) {
        when(v?.id){
            R.id.radio_button1 -> {
                selectedOptionView(radio_button1, 1)
            }
            R.id.radio_button2 -> {
                selectedOptionView(radio_button2, 2)
            }
            R.id.radio_button3 -> {
                selectedOptionView(radio_button3, 3)
            }
            R.id.radio_button4 -> {
                selectedOptionView(radio_button4, 4)
            }
            R.id.btn_submit -> {
                if (mSelectedOptionPosition == 0) {
                    if (radio_group.checkedRadioButtonId == -1 && btn_submit.isPressed && btn_submit.text == "SUBMIT") {
                        Toast.makeText(
                            getApplicationContext(),
                            "Please select an answer",
                            Toast.LENGTH_SHORT
                        ).show();
                    } else {
                        radio_group.clearCheck()
                        mCurrentPosition++
                    }
                    when {
                        mCurrentPosition <= mQuestionsList!!.size -> {
                            setQuestion()
                        }
                        else -> {
                            val intent = Intent(this, ResultsActivity::class.java)
                            intent.putExtra(ConstantsAnalysis.TOTAL_CORRECT, mCorrectAnswers)
                            intent.putExtra(ConstantsAnalysis.TOTAL_OPP, mQuestionsList!!.size)
                            startActivity(intent)
                        }
                    }
                } else {
                    val questions3 = mQuestionsList?.get(mCurrentPosition - 1)
                    if (questions3!!.correctAnswer != mSelectedOptionPosition) {
                        answerView(mSelectedOptionPosition, R.drawable.incorrect_option_border_bg)
                        val dialogBuilder = AlertDialog.Builder(this)
                        when {
                            questions3.dialogBox!=null && questions3.dialogBox2!=null -> {
                                dialogBuilder.setMessage("Test \n\n" + getString(questions3.dialogBox) + "\n\n" + getString(questions3.dialogBox2))
                                    .setCancelable(true)
                                    .setNegativeButton("Close") { dialog, id -> dialog.cancel()
                                    }
                                val alert = dialogBuilder.create()
                                alert.show()
                            }
                            questions3.dialogBox!=null && questions3.dialogBox2 == null -> {
                                dialogBuilder.setMessage("Test \n\n" + getString(questions3.dialogBox))
                                    .setCancelable(true)
                                    .setNegativeButton("Close") { dialog, id -> dialog.cancel()
                                    }
                                val alert = dialogBuilder.create()
                                alert.show()
                            }
                        }
                    } else {
                        mCorrectAnswers++
                    }
                    answerView(questions3.correctAnswer, R.drawable.correct_option_border)
                    if (mCurrentPosition == mQuestionsList!!.size) {
                        btn_submit.text = "FINISH"
                    } else {
                        btn_submit.text = "NEXT QUESTION"
                    }
                    mSelectedOptionPosition = 0
                }
            }
        }
    }

【问题讨论】:

  • 乍一看还不错。代码有问题吗?
  • 它会运行,但不会随机播放选项。
  • 我有一个测验应用程序。当我设置一个问题时,我将答案设置为一个变量。然后,我以编程方式创建一个包含正确答案的可能答案列表,随机排列列表(因此顺序会有所不同)并将它们应用于按钮。然后在我的按钮监听器中,我只需检查变量的按钮文本,以了解他们的答案是否与正确答案匹配。当您洗牌时,您不需要新变量,您可以使用 options.shuffle() 调用您已经创建的列表

标签: android kotlin shuffle


【解决方案1】:

有一个很好的方法是通过将QuestionAnswer 分开来分隔对象。 我的意思是,您可以放置​​一个自定义答案对象列表,而不是问题有一个字符串列表作为答案

更具体地说,您必须创建一个像这样的答案类。

Answer.kt

data class Answer(
        val answer: String,
        val isCorrect: Boolean,
        // You can add other attributes
)

然后你必须改变你的Question 类是这样的

问题.kt

data class Question(
        val id: Int,
        val image: Int?,
        val answers: MutableList<Answer>, // or You can add them as 4 separate answer objects
        val dialogBox: Int?,
        val dialogBox2: Int?
)

然后通过获取用户选择的答案并调用来更容易洗牌和识别正确答案

if (answer.isCorrect()){
   // Whatever
}

通过实现这一点,洗牌将变得如此简单

val question = Question( /* Question Defenition*/)
question.answers.shuffle()

编辑

例如,您可以在 MainActivity onCreate 中定义这样的问题

val answersList1 = mutableListOf(
    Answer("answer1", false),
    Answer("answer2", false),
    Answer("answer3", true), // This is the correct answer
    Answer("answer4", false),
)
val question1 = Question(
    id = 0,
    image = R.drawable.image,
    answers = answersList,
    dialogBox = 1,
    dialogBox2 = 2
)

然后通过更改它们的文本属性来重命名您的 RadioButtons,或者如果您使用的是 TextViews,您也可以重命名它们。


radio_button0.text = answersList1[0].answer
radio_button1.text = answersList1[1].answer
// add 4 radio buttons

然后在您的onClick 回调中,您可以触发点击并像这样识别正确答案

override fun onClick(v: View?) {
    when (v?.id) {
        R.id.radio_button0 -> {
            if (answersList1[0].isCorrect)
            // the answer is correct
        }
        R.id.radio_button1 -> {
            if (answersList1[1].isCorrect)
                // the answer is correct
                // Do something
        }
    }
}

【讨论】:

  • 在数据类 Answer() 中是否需要包含四个 val 答案和四个布尔值? (即val answer1: String, val isCorrect: Boolean, val answer2: String, val isCorrect: Boolean 等)我将如何更改object QuestionsAnalysis 以适应这些更改?如何在Questions Activity 中引用正确答案?
  • 我会尝试做一个简单的例子
  • 感谢您写出这篇文章。不幸的是,我有大约 100 个问题,并且不希望将它们全部重写以将问题与答案分开。有没有办法使用我的代码中的随机播放功能,或者可以为字符串设置正确的答案,这样我就不必担心正确的选项位置?
猜你喜欢
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2022-06-22
  • 2018-11-16
相关资源
最近更新 更多