我怎样才能让这个功能停止重复问题? [英] How would I get this function to stop repeating questions?
问题描述
我正在制作一个测验应用程序,我有一个不断重复问题的功能,这就是功能:
I'm making a quiz app and I have a function that keeps repeating questions, this is the function:
func updateQuestion(){
if questionNumber < allQuestions.list.count{
allQuestions.list.shuffle()
questionsView.text = allQuestions.list[questionNumber].question
optionA.setTitle(allQuestions.list[questionNumber].optionA, for: UIControl.State.normal)
optionB.setTitle(allQuestions.list[questionNumber].optionB, for: UIControl.State.normal)
optionC.setTitle(allQuestions.list[questionNumber].optionC, for: UIControl.State.normal)
optionD.setTitle(allQuestions.list[questionNumber].optionD, for: UIControl.State.normal)
selectedAnswer = allQuestions.list[questionNumber].correctAnswer
}
我的问题存储在另一个名为 QuestionsBank 的类中,我怎样才能让它们不重复?
My questions are stored in another class named QuestionsBank, how would I be able to get them to not repeat?
class QuestionBank {
var list = [Question]()
init() {
list.append(Question(questionText: "What is his middle name?", choiceA: "E***k", choiceB: "E******o", choiceC: "G******s", choiceD: "He doesn't have a middle name", answer: 2))
list.append(Question(questionText: "What is the name of his first dog?", choiceA: "Neka", choiceB: "Sadie", choiceC: "Bear", choiceD: "Jeffery", answer: 3))
list.append(Question(questionText: "What is his energy drink of choice?", choiceA: "Monster", choiceB: "Reign", choiceC: "Rockstar", choiceD: "Bang", answer: 2))
}
}
我觉得我需要以某种方式为每个问题分配标签以识别它们,但我不知道如何去做.我是编程新手,因此我们将不胜感激,尽管我可能会提出很多问题.
I feel like I would somehow need to assign tags to each of the questions to identify them but I don't know how to go about that. I'm new to programming so any help would be appreciated, although I may ask a lot of questions.
推荐答案
你需要做的就是不要在 updateQuestion
函数中打乱数组,而是在你的 末尾执行此操作>QuestionBank
init 方法.
All you need to do is to not shuffle the array in the updateQuestion
function and instead do this at the end of your QuestionBank
init method.
现在问题是随机排序的,您使用 questionNumber
已经必须跟踪下一个问题.
Now the question are randomly ordered and you use the questionNumber
you already have to keep track of the next question.
另一个类似的解决方案是让QuestionBank
负责处理问题
Another but similar solution is to let the QuestionBank
take responsibility to handle the question
class QuestionBank {
private let list = [Question]()
private var index: Int
init() {
var temp = [Question(questionText: "What is his middle name?", choiceA: "E***k", choiceB: "E******o", choiceC: "G******s", choiceD: "He doesn't have a middle name", answer: 2),
Question(questionText: "What is the name of his first dog?", choiceA: "Neka", choiceB: "Sadie", choiceC: "Bear", choiceD: "Jeffery", answer: 3),
Question(questionText: "What is his energy drink of choice?", choiceA: "Monster", choiceB: "Reign", choiceC: "Rockstar", choiceD: "Bang", answer: 2)]
index = 0
list = temp.shuffled()
}
func nextQuestion() -> Question? {
guard index < list.count else { return nil }
let question = list[index]
index += 1
return question
}
}
然后更新函数将是
func updateQuestion(){
if let question = allQuestions.nextQuestion() {
questionsView.text = question.questionText
optionA.setTitle(question.optionA, for: UIControl.State.normal)
optionB.setTitle(question.optionB, for: UIControl.State.normal)
optionC.setTitle(question.optionC, for: UIControl.State.normal)
optionD.setTitle(question.optionD, for: UIControl.State.normal)
selectedAnswer = question.correctAnswer
} else {
// No more questions to ask, handle this here
}
}
这篇关于我怎样才能让这个功能停止重复问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!