gpt4 book ai didi

android - Kotlin-在随机列表中获取新的项目索引

转载 作者:行者123 更新时间:2023-12-02 13:39:38 24 4
gpt4 key购买 nike

我有一个选择题,每个答案有4个选择。在具有问题和选择的ArrayList中,将正确答案设置为正确选项的索引。我想打乱选择,但不确定如何确定正确答案的新索引。有什么想法吗?
问题对象

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_1995, null)
questionList.addAll(listOf(q1))
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?)
随机选择
val ansorder = arrayOf(question.option1, question.option2, question.option3, question.option4)
ansorder.shuffle()
radio_button1.text = ansorder[0]
radio_button2.text = ansorder[1]
radio_button3.text = ansorder[2]
radio_button4.text = ansorder[3]
检查答案选择
if (questions3!!.correctAnswer != mSelectedOptionPosition) {
//do x
}
编辑(由于 correct answer是一个字符串,并且改组后的索引 answerView(questions3.correctAnswer, R.drawable.correct_option_border会更改。
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
private var mSelectedOptionText: String? = null

private fun shuffle() {
val question = mQuestionsList!![mCurrentPosition - 1]
val ansorder = arrayOf(question.option1, question.option2, question.option3, question.option4)
ansorder.shuffle()
radio_button1.text = ansorder[0]
radio_button2.text = ansorder[1]
radio_button3.text = ansorder[2]
radio_button4.text = ansorder[3]
}

override fun onClick(v: View?) {
when(v?.id){
R.id.radio_button1 -> { selectedOptionView(radio_button1, 1)
mSelectedOptionText = radio_button1.text as String?
}
R.id.radio_button2 -> { selectedOptionView(radio_button2, 2)
mSelectedOptionText = radio_button2.text as String?
}
R.id.radio_button3 -> { selectedOptionView(radio_button3, 3)
mSelectedOptionText = radio_button3.text as String?
}
R.id.radio_button4 -> { selectedOptionView(radio_button4, 4)
mSelectedOptionText = radio_button4.text as String?
}

R.id.btn_submit -> {
val questions3 = mQuestionsList?.get(mCurrentPosition - 1)
if (questions3!!.correctAnswer != mSelectedOptionText) {
} else {
mCorrectAnswers++
}
answerView(questions3.correctAnswer, R.drawable.correct_option_border)

private fun answerView(answer: Int, drawableView: Int) {
when(answer){
1 -> {
radio_button1.background = ContextCompat.getDrawable(this, drawableView)
}
2 -> {
radio_button2.background = ContextCompat.getDrawable(this, drawableView)
}
3 -> {
radio_button3.background = ContextCompat.getDrawable(this, drawableView)
}
4 -> {
radio_button4.background = ContextCompat.getDrawable(this, drawableView)
}
}
}

最佳答案

我真的建议仅创建一个像这样的数据类:

data class QuestionOption(val question:String, val isCorrect = false)
之后,您可以按自己喜欢的任何方式进行随机播放,只需检查所选的QuestionOption是否将isCorrect设置为true。您可以获得很多好处,逻辑也变得更加简单。
编辑:
为了更轻松地以这种方式声明问题:
通常,如果在代码中添加问题,则只需要所需的必要代码。为此,您可以声明一个好的构造函数或一个将您的值基本上映射到构造函数的函数。你的情况我会说
data class Questions3(
val id: Int, val question: String, val option1: String, val option2: String,
val option3: String, val correctOption: String, val image: Int?=null,val dialogBox1: Int?=null,val dialogBox2: Int?=null)
(请注意可选参数的最后使用方式,由于默认情况下它们为空,因此您也无需指定它们)
从理论上讲,您也可以(不太干净,但很容易)只是将选项1-3和correctOption混洗,然后比较正确的String是否匹配所选的String。
否则,就像我说的那样,您始终可以创建用于映射内容的逻辑。在这里,您可以从构造函数映射到另一个构造函数,与返回完成对象的函数相同。

关于android - Kotlin-在随机列表中获取新的项目索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64599402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com