therealbluepandabear
09/22/2021, 10:45 PMimport kotlin.random.Random
fun main(args: Array<String>) {
val quizCreator = QuizCreator()
quizCreator.makeQuiz()
}
class QuizCreator {
private val quizInstance = Quiz("", listOf())
fun makeQuiz() {
println("Choose quiz name:")
quizInstance.quizName = readLine().toString()
while (true) {
println("New Question? (Y/n)")
if (readLine()?.lowercase() == "y") {
var question = Question(null, null)
println("Question name?")
question.questionText = readLine()
println("Question answer?")
question.questionAnswer = readLine()
quizInstance.questions.toMutableList().add(question)
println(quizInstance.questions[0])
} else {
println("OK")
quizInstance.startQuiz()
break
}
}
}
}
data class Quiz(var quizName: String, val questions: List<Question>) {
fun startQuiz() {
for (question in questions) {
println(question.questionText)
val answer = readLine()
if (answer?.lowercase() != question.questionAnswer?.lowercase()) {
println("Incorrect, the answer was ${question.questionAnswer}")
} else {
println("Correct")
}
}
}
}
data class Question(var questionText: String?, var questionAnswer: String?)
ok so I have this code, and for some reason when running this it's saying that the list of questions is empty:
Choose quiz name:
j
New Question? (Y/n)
y
Question name?
j
Question answer?
j
Exception in thread "main" java.lang.IndexOutOfBoundsException: Empty list doesn't contain element at index 0.
at kotlin.collections.EmptyList.get(Collections.kt:36)
at kotlin.collections.EmptyList.get(Collections.kt:24)
at QuizCreator.makeQuiz(Main.kt:32)
at MainKt.main(Main.kt:5)
Process finished with exit code 1
ephemient
09/22/2021, 11:13 PM.toMutableList()
creates a new list, and mutating it does nothing to the original