swift - 如何将用户输入与 Array 的洗牌对象进行比较
问题描述
我有一堆与我的自定义类问题相关的问题
class QuestionBank {
var list = [Question]()
init() {
// Creating a quiz item and appending it to the list
let item = Question(text: "2² = ", correctAnswer: 4)
list.append(item)
list.append(Question(text: "3² = ", correctAnswer: 9))
list.append(Question(text: "4² = ", correctAnswer: 16))
list.append(Question(text: "5² = ", correctAnswer: 25))
// and so on...
}
}
这是这个自定义类问题
let answer : Int
let questionText : String
init(text : String, correctAnswer : Int) {
questionText = text
answer = correctAnswer
}
我有一个按钮,当你按下它时,所有问题(等于 QuestionBank)正在被洗牌。接下来,用户输入他的答案
@IBAction func truePressed(_ sender: Any) {
allQuestions.list.shuffle()
questionLabel.text = allQuestions.list[questionNumber].questionText
if Int(textField.text!) == allQuestions.list[questionNumber].answer {
print("yes")
}
textField.text = ""
}
问题是控制台不输出任何东西。我认为这是因为当 shuffle() 发生时,“questionText”和“answer”都在改组,或者我错了?如果我是对的,我该如何更改“allQuestions”随机播放,以便在按下按钮时 textField 读取用户输入,然后将 input(textField.text) 与“allQuestions.list[questionNumber].answer”进行比较?
解决方案
推荐阅读
- html - 我的 SVG 似乎有数据,但没有呈现
- java - 无法将 [...ReloadableResourceBundleMessageSource] 转换为所需类型 [...ResourceBundleMessageSource]
- apollo - 如何为不使用 Apollo 引擎的项目正确配置 `apollo` CLI?
- python - 在 Flask 中使用 for 循环更新数据库?
- asp.net - Web 窗体用户控制事件,需要在页面加载后添加
- r - R:如何使用别名调用数据框列?
- c# - 如何在 MVC 主控制器中将 http post 请求数据读取为 JSON?
- memcached - 是否可以选择使用 spring 缓存从缓存中执行 getBulk
- java - 即使关闭 InputStream 也无法删除文件
- python - Numpy比较两个3d数组并找到相同的数组