gpt4 book ai didi

ios - Swift 4 - 测验应用程序随机问题

转载 作者:行者123 更新时间:2023-11-30 11:50:49 25 4
gpt4 key购买 nike

我是 Swift 和 Xcode 的初学者。预先感谢您的时间和帮助:)

我正在做一个测验应用程序,我尝试提出随机问题...我有一个函数来生成随机数,我试图在 viewDidLoad() 中应用它...不幸的是我无法猜测如何使用该信息在他的变量“var currentQuestion = 0”

这是代码:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

generateRandomNumber(0, 9, 10)
}

override func viewDidAppear(_ animated: Bool) {
newQuestion()
}


// Quiz//

let questions = ["Q1","Q2","Q3","Q4","Q5"]

let answers = [["A","2","3"],["B","2","3"],["C","2","3"],["D","2","3"],["E","2","3"]]


//Variables
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0


//Generate Random Numbers
func generateRandomNumber(_ from:Int, _ to:Int, _ qut:Int?) -> [Int] {
var myRandomNumbers = [Int]() //All our generated numbers
var numberOfNumbers = qut //How many numbers to generate

let lower = UInt32(from) //Generate from this number..
let higher = UInt32(to+1) //To this one

if numberOfNumbers == nil || numberOfNumbers! > (to-from) + 1 {
numberOfNumbers = (to-from) + 1
}

while myRandomNumbers.count != numberOfNumbers {
let myNumber = arc4random_uniform(higher - lower) + lower

if !myRandomNumbers.contains(Int(myNumber)) {
myRandomNumbers.append(Int(myNumber))
}
}
return myRandomNumbers
}


//Label
@IBOutlet weak var lbl: UILabel!


//Button
@IBAction func action(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerPlacement)) {
print ("RIGHT!")
} else {
print ("WRONG!!!!!!")
}

if (currentQuestion != questions.count) {
newQuestion()
} else {
}
}

//Function that displays new question
func newQuestion() {
lbl.text = questions[currentQuestion]

rightAnswerPlacement = arc4random_uniform(3)+1

//Create a button
var button:UIButton = UIButton()

var x = 1

for i in 1...3 {
//Create a button
button = view.viewWithTag(i) as! UIButton

if (i == Int(rightAnswerPlacement)) {
button.setTitle(answers[currentQuestion][0], for: .normal)
} else {
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}

知道如何解决吗?

最佳答案

首先,您需要存储生成的随机问题数组(旁注,您似乎尝试生成 10 个随机数,但只有 5 个问题)。

然后,您可以使用它来访问数组中的问题,而不是直接使用 currentQuestion。

所以将其更改为:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

questionArray = generateRandomNumber(0, 9, 10)
}

override func viewDidAppear(_ animated: Bool) {
newQuestion()
}


// Quiz//

let questions = ["Q1","Q2","Q3","Q4","Q5"]

let answers = [["A","2","3"],["B","2","3"],["C","2","3"],["D","2","3"],["E","2","3"]]


//Variables
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0
var questionArray: [Int] = [] // Just an initial value


//Generate Random Numbers
func generateRandomNumber(_ from:Int, _ to:Int, _ qut:Int?) -> [Int] {
var myRandomNumbers = [Int]() //All our generated numbers
var numberOfNumbers = qut //How many numbers to generate

let lower = UInt32(from) //Generate from this number..
let higher = UInt32(to+1) //To this one

if numberOfNumbers == nil || numberOfNumbers! > (to-from) + 1 {
numberOfNumbers = (to-from) + 1
}

while myRandomNumbers.count != numberOfNumbers {
let myNumber = arc4random_uniform(higher - lower) + lower

if !myRandomNumbers.contains(Int(myNumber)) {
myRandomNumbers.append(Int(myNumber))
}
}
return myRandomNumbers
}


//Label
@IBOutlet weak var lbl: UILabel!


//Button
@IBAction func action(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerPlacement)) {
print ("RIGHT!")
} else {
print ("WRONG!!!!!!")
}

if (currentQuestion != questions.count) {
newQuestion()
} else {
}
}

//Function that displays new question
func newQuestion() {
lbl.text = questions[questionArray[currentQuestion]]

rightAnswerPlacement = arc4random_uniform(3)+1

//Create a button
var button:UIButton = UIButton()

var x = 1

for i in 1...3 {
//Create a button
button = view.viewWithTag(i) as! UIButton

if (i == Int(rightAnswerPlacement)) {
button.setTitle(answers[questionArray[currentQuestion]][0], for: .normal)
} else {
button.setTitle(answers[questionArray[currentQuestion]][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}

(我认为代码是正确的,但我目前正在 Xcode 中运行一些测试,因此无法检查。如果有问题请发表评论。)

关于ios - Swift 4 - 测验应用程序随机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48328633/

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