gpt4 book ai didi

ios - 通过遍历字典显示随机问题

转载 作者:行者123 更新时间:2023-11-29 01:57:58 25 4
gpt4 key购买 nike

我通过尝试制作问答游戏开始学习 Swift。但我坚持尝试显示字典中的随机问题。我使用字典的原因是因为我正在做的测验是基于具有不同符号的化学元素。

所以根据选择的难度,我创建了四个不同的词典:

//Defining the dictionaries per difficulty
var easy: [String: String] = [
"H": "Waterstof",
"He": "Helium",
"Li": "Lithium",
"B": "Boor",
"C": "Koolstof",
"O": "Zuurstof",
"Ne": "Neon",
"Al": "Aliminium",
"Si": "Silicium",
"K": "Kalium",
"Fe": "Ijzer",
"Ni": "Nikkel",
"Zn": "Zink",
"Cd": "Cadmium",
"I": "Jood"
]
var normal: [String: String] = [
"N": "Stikstof",
"F": "Fluor",
"Na": "Natrium",
"Mg": "Magnesium",
"P": "Fosfor",
"CI": "Chloor",
"Ar": "Argon",
"S": "Zwavel",
"Ca": "Calcium",
"Cu": "Koper",
"Au": "Goud",
"Br": "Broom",
"Ag": "Zilver",
"Pt": "Platina",
"Ba": "Barium"
]

等等..(困难的,极端的)

这是我的 viewDidLoad() 方法:

override func viewDidLoad() {

switch (self.selectedDifficultyByUser){
case "Makkelijk":// Means 'easy' in dutch
//Assigning the easy dictionary to a temporary dictionary
self.temp = self.easy
case "Normaal":
//Assigning the normal dictionary to a temporary dictionary
self.temp = self.normal
case "Moeilijk":
//Assigning the hard dictionary to a temporary dictionary
self.temp = self.moeilijk
case "Extreem":
//Assigning the extreme dictionary to a temporary dictionary
self.temp = self.extreem
default:
println("Something went wrong")

}

super.viewDidLoad()
self.answers = [self.btn1, self.btn2, self.btn3, self.btn4]

pickRandomElement()
randomizeAnswers()

let updateTime : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: updateTime, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()

}

如您所见,我正在将我所有的按钮分配给数组“answers”,我很快就会循环访问它。

然后将调用函数 pickRandomElement,如下所示:

func pickRandomElement() -> () {
//First here we make sure that the 'answerOptions' Array is cleared,
//Because it could cause that the correct answer won't be showed.
answerOptions = [String]()


if self.selectedDifficultyByUser == "Makkelijk" {
let index: Int = Int(arc4random_uniform(UInt32(easy.count)))

let symbol = Array(easy.keys)[index]
let element = Array(easy.values)[index]

self.currentQuestion = symbol
self.correctAnswer = element

//Assign the correctAnswer to the answerOptions array
self.answerOptions.append(element)

//Show the question to the user
self.questionLabel.text = self.currentQuestion

//remove the correctanswer from the dictionary, to
//make sure that the answers won't be duplicated
self.easy[symbol] = nil
self.easy[element] = nil

for (var i = 0; i < 3; i++) {
let randomIndex: Int = Int(arc4random_uniform(UInt32(easy.count)))

let optionSymbol = Array(easy.keys)[randomIndex]
let optionElement = Array(easy.values)[randomIndex]

self.answerOptions.append(optionElement)

//Removing 'optionSymbol' and 'optionElement' from the array
//to prevent duplicated answer
self.easy[optionElement] = nil
self.easy[optionSymbol] = nil
}
self.easy = self.temp //Filling the 'easy' array with the temporary array

}
}

我遇到的问题是在上面显示的 for 循环中。我循环三次以选择随机元素并将它们显示给用户。在此过程中,我将从(简单)数组中删除随机选择的元素以防止重复答案。因为我要从 easy 数组中删除这些元素,所以我将我在开始时创建的临时数组分配回 easy 数组。

如果我这样做,整个 easy 数组将在三轮左右后为空。如果我相反的事情,我会得到一个无限循环。

如果我以错误的方式执行此操作或帮助我解决此问题,有人可以将我引向正确的方向吗?

提前致谢!

已更新

Firo 的解决方案非常适合我。但我知道面临另一个错误。也就是说,pickRandomElement 函数有时会多次返回所问的问题。我试着制作另一本字典,把提出的问题放在那里,然后检查是否已经有人问过这个问题。然而,随着回答问题数量的增加,这将需要对每个问题进行线性增加的查找,直到达到所有查找。有人知道我该如何解决这个问题吗? –

最佳答案

因此,在没有您的所有代码的情况下,我只能给您一个有根据的猜测,以了解用您当前的代码处理无限循环情况的好方法。

这里防止无限循环的一个简单方法是确认您还有其他问题要问。保留一个实例值以检查您还想问多少问题。

var remainingSteps = 5

然后当用户回答正确的问题时,在提出下一个问题之前检查它

func userAnsweredQuestionCorrectly() {
if self.remainingSteps > 0 {
// Present next question
// Decrement remaining steps
self.remainingSteps--
else {
// Done asking questions, back to menu (or whatever is next)
}
}

关于ios - 通过遍历字典显示随机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30644333/

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