我创建了一个包含随机问题的类,但是switch语句没有结束,问题继续循环。我如何防止这种情况发生,并在所有4个问题随机完成后显示另一页?
import UIKit
class ThirdViewController: UIViewController {
@IBOutlet weak var Question: UILabel!
@IBOutlet weak var Ans1: UIButton!
@IBOutlet weak var Ans2: UIButton!
@IBOutlet weak var Ans3: UIButton!
@IBOutlet weak var Ans4: UIButton!
@IBOutlet weak var Result: UILabel!
@IBOutlet weak var Next: UIButton!
var correctAns = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
Random()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Random(){
var RandomNumber = arc4random() % 4
RandomNumber += 1
switch (RandomNumber) {
case 1:
Question.text = "Where does minal lives?"
Ans1.setTitle("dubai", for: UIControlState.normal)
Ans2.setTitle("london", for: UIControlState.normal)
Ans3.setTitle("india", for: UIControlState.normal)
Ans4.setTitle("japan", for: UIControlState.normal)
correctAns = "2"
break
case 2:
Question.text = "What is my name?"
Ans1.setTitle("Sunil", for: UIControlState.normal)
Ans2.setTitle("Harish", for: UIControlState.normal)
Ans3.setTitle("Rohit", for: UIControlState.normal)
Ans4.setTitle("Minal", for: UIControlState.normal)
correctAns = "4"
break
case 3:
Question.text = "How old are you?"
Ans1.setTitle("22", for: UIControlState.normal)
Ans2.setTitle("32", for: UIControlState.normal)
Ans3.setTitle("21", for: UIControlState.normal)
Ans4.setTitle("28", for: UIControlState.normal)
correctAns = "1"
break
case 4:
Question.text = "What are you studying?"
Ans1.setTitle("Computer Science", for: UIControlState.normal)
Ans2.setTitle("Java", for: UIControlState.normal)
Ans3.setTitle("Bio", for: UIControlState.normal)
Ans4.setTitle("Business", for: UIControlState.normal)
correctAns = "3"
break
default:
Result.text = "Finished"
break
}
}
func Hide(){
Result.isHidden = true
Next.isHidden = true
}
func Unhide(){
Result.isHidden = false
Next.isHidden = false
}
@IBAction func Ans1Action(_ sender: Any) {
Unhide()
if (correctAns == "1"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func Ans2Action(_ sender: Any) {
Unhide()
if (correctAns == "2"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func Ans3Action(_ sender: Any) {
Unhide()
if (correctAns == "3"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func Ans4Action(_ sender: Any) {
Unhide()
if (correctAns == "4"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func NextAction(_ sender: Any) {
Random()
Hide()
}
}
答案 0 :(得分:0)
您需要保留已经提出的问题数量的计数器。 switch (RandomNumber)
将始终输入其中一个case
语句,因为RandomNumber
始终位于1...4
范围内。
因此,您需要做的是添加一个实例变量
private var questionCounter = 0
并修改NextAction
函数,如下所示:
@IBAction func NextAction(_ sender: Any) {
questionCounter += 1
if questionCounter >= 4 {
Result.text = "Finished"
// or whatever...
}
else {
Random()
Hide()
}
}
请注意,每次执行arc4random()
时,同一问题可能会出现多次。
答案 1 :(得分:0)
创建一个从1到4的Int索引数组。
从数组中提取元素并显示该项目。当数组为空时,停止:
这样的事情:
var questionIndexes = [1, 2, 3, 4]
if questionIndexes.count > 0 {
let random = Int(arc4random_uniform(Uint32(questionIndexes.count)))
let index = questionIndexes.remove(at: index)
askQuestionAtIndex(index)
}
(您必须调整代码以适应现有代码,但这是基本想法。)