I've been an introductory programmer for about a month.
I'm creating a quiz app on Swift4.
I'd like to ask random questions without duplicating the quiz application questions, but the same questions are repeated and it doesn't work.
Please let me know if there is anyone who can tell me how to randomly ask questions without duplicating them.
Prepare the questions for LabelQuestion, Button1 through Button3, let LabelComment see if the selected answer is correct, and press ButtonNextQuestion to display the following questions and answers:
The code is as follows:
import UIKit
classViewController:UIViewController {
@IBOutlet weak var LabelQuestion: UILabel!
@IBOutlet weak var Button 1: UIButton!
@IBOutlet weak var Button 2: UIButton!
@IBOutlet weak var Button 3: UIButton!
@IBOutlet weak var LabelComment: UILabel!
@IBOutlet weak var ButtonNextQuestion: UIButton!
varcorrectAnswer=String()
override func viewDidLoad(){
super.viewDidLoad()
randomQestions()
hide()
ButtonNextQuestion.setTitle("NEXT QUESTION", for:UIControlState.normal)
}
override funcdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
}
funcrandomQestions(){
var question = ["case1", "case2", "case3", "case4" ]
var result: String = [ ]
for_in 0...3 {
let randomNumber = Int(arc4 random()%UInt32(question.count))
result.append (question [randomNumber])
question.remove(at:(randomNumber))
switch(randomNumber) {
case1:
LabelQuestion.text="1+1=?"
Button1.setTitle("1", for:UIControlState.normal)
Button2.setTitle("2", for:UIControlState.normal)
Button3.setTitle("3", for:UIControlState.normal)
correctAnswer="1"
break
case2:
LabelQuestion.text="3+3=?"
Button1.setTitle("4", for:UIControlState.normal)
Button2.setTitle("5", for:UIControlState.normal)
Button3.setTitle("6", for:UIControlState.normal)
correctAnswer="3"
break
case3:
LabelQuestion.text="4+4=?"
Button1.setTitle("7", for:UIControlState.normal)
Button2.setTitle("8", for:UIControlState.normal)
Button3.setTitle("9", for:UIControlState.normal)
correctAnswer="2"
break
case4:
LabelQuestion.text="6+6=?"
Button1.setTitle("10", for:UIControlState.normal)
Button2.setTitle("11", for:UIControlState.normal)
Button3.setTitle("12", for:UIControlState.normal)
correctAnswer="3"
break
default:
break
}
}
}
US>funchide(){
LabelComment.isHidden=true
}
func unhide(){
LabelComment.isHidden=false
}
@ IBAction func Button 1 Action(_sender:Any) {
unhide()
if(correctAnswer=="1"){
LabelComment.text=" ""
} else{
LabelComment.text="×"
}
}
@ IBAction func Button 2 Action(_sender:Any) {
unhide()
if(correctAnswer=="2"){
LabelComment.text=" ""
} else{
LabelComment.text="×"
}
}
@ IBAction func Button 3 Action(_sender:Any) {
unhide()
if(correctAnswer=="3"){
LabelComment.text=" ""
} else{
LabelComment.text="×"
}
}
@ IBAction func ButtonNextQuestion(_sender:Any){
randomQestions()
hide()
}
}
Thank you in advance.
<Additional>
Thank you for your reply.
You told me
How do I shuffle Array as a pseudo-random number with swift?
I tried rewriting it with reference to , but when I tried using it as it is, I got 3 errors.
I tried to fix the error by erasing the count and changing the number 0..<(count-1) but it didn't work.
Below is a rewriting between var question=["case1", "case2", "case3", "case4"] and "switch(randomNumber){".
var question=["case1", "case2", "case3", "case4" ]
extension Array {
mutating func shuffle() {
for in 0..<(count-1){
let j = Int(arc4 random_uniform(UInt32(count-i))))) + i
swap(&self[i], &self[j])
}
}
switch(randomNumber) {
Declaration is only valid at file scope
under extension Array {
"Use of unresolved identifier 'count'"
where for i in 0..<(count-1){
ExExpected declaration <
on switch(randomNumber){
You will receive three error messages:
How do I rewrite the code to erase the error and shuffle it?
I would appreciate it if you could teach me.
Thank you in advance.
In principle, random numbers can't be called random numbers if they have the same number or only different numbers, so let's reverse the idea here.
When there is a list of problems, shuffle the order in advance and use it in order.
How do I shuffle an array with a pseudo-random number in swift?
Repeat
Select the appropriate problem from the problem list using random numbers and discard it.
To shuffle questions from the swift quiz app
© 2024 OneMinuteCode. All rights reserved.