gpt4 book ai didi

ios - 我怎样才能让这个功能停止重复问题?

转载 作者:行者123 更新时间:2023-11-29 05:17:36 25 4
gpt4 key购买 nike

我正在制作一个测验应用程序,我有一个不断重复问题的功能,这是功能:

    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 的类中,我怎样才能让它们不重复?

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))
}
}

我觉得我需要以某种方式为每个问题分配标签来识别它们,但我不知道如何去做。我是编程新手,因此我们将不胜感激,尽管我可能会问很多问题。

最佳答案

您需要做的就是不要在 updateQuestion 函数中打乱数组,而是在 QuestionBank init 方法的末尾执行此操作。

现在问题是随机排序的,您可以使用已经必须跟踪下一个问题的questionNumber

另一个类似的解决方案是让QuestionBank负责处理问题

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
}
}

关于ios - 我怎样才能让这个功能停止重复问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59015682/

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