簡體   English   中英

Kotlin - 獲取隨機列表中項目的新索引

[英]Kotlin - Get new index of item in shuffled list

我有一個多項選擇測驗,每個答案有 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 和正確選項,然后比較正確選項字符串是否與所選字符串匹配。

否則,正如我所說,您始終可以為映射內容創建邏輯。 在這里,您可以從構造函數映射到另一個構造函數,與返回完成對象的函數相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM