Ios 如何使Swift中的切换情况不会发生多次?
我正在制作一个乘法测验应用程序,它使用不同级别的开关 如果我只想在这个切换中问两个问题,我如何使已经问过的案例/问题不会被问两次Ios 如何使Swift中的切换情况不会发生多次?,ios,iphone,swift,Ios,Iphone,Swift,我正在制作一个乘法测验应用程序,它使用不同级别的开关 如果我只想在这个切换中问两个问题,我如何使已经问过的案例/问题不会被问两次 var RandomNumber = arc4random() % 4 RandomNumber += 1 switch(RandomNumber){ case 1: QuestionLabel.text = "What is 4 x 2?" Button1.setTitle("2", forSta
var RandomNumber = arc4random() % 4
RandomNumber += 1
switch(RandomNumber){
case 1:
QuestionLabel.text = "What is 4 x 2?"
Button1.setTitle("2", forState: UIControlState.Normal)
Button2.setTitle("4", forState: UIControlState.Normal)
Button3.setTitle("8", forState: UIControlState.Normal)
Button4.setTitle("12", forState: UIControlState.Normal)
CorrectAnswer = "3"
break
case 2:
QuestionLabel.text = "What is 3 x 3?"
Button1.setTitle("6", forState: UIControlState.Normal)
Button2.setTitle("9", forState: UIControlState.Normal)
Button3.setTitle("12", forState: UIControlState.Normal)
Button4.setTitle("33", forState: UIControlState.Normal)
CorrectAnswer = "2"
break
case 3:
QuestionLabel.text = "What is 3 x 2?"
Button1.setTitle("6", forState: UIControlState.Normal)
Button2.setTitle("8", forState: UIControlState.Normal)
Button3.setTitle("9", forState: UIControlState.Normal)
Button4.setTitle("13", forState: UIControlState.Normal)
CorrectAnswer = "1"
break
case 4:
QuestionLabel.text = "What is 4 x 3?"
Button1.setTitle("8", forState: UIControlState.Normal)
Button2.setTitle("9", forState: UIControlState.Normal)
Button3.setTitle("11", forState: UIControlState.Normal)
Button4.setTitle("12", forState: UIControlState.Normal)
CorrectAnswer = "4"
break
default:
break
}
请原谅我的大规模重构,但快速处理这段代码很有趣:) 我做了以下调整:
- 结构表示问题,枚举表示正确/错误
- 用于删除随机元素的数组扩展
- 使用按钮标记来标识具有选项的按钮
var RandomNumber = arc4random() % 4
RandomNumber += 1
switch(RandomNumber){
case 1:
QuestionLabel.text = "What is 4 x 2?"
Button1.setTitle("2", forState: UIControlState.Normal)
Button2.setTitle("4", forState: UIControlState.Normal)
Button3.setTitle("8", forState: UIControlState.Normal)
Button4.setTitle("12", forState: UIControlState.Normal)
CorrectAnswer = "3"
break
case 2:
QuestionLabel.text = "What is 3 x 3?"
Button1.setTitle("6", forState: UIControlState.Normal)
Button2.setTitle("9", forState: UIControlState.Normal)
Button3.setTitle("12", forState: UIControlState.Normal)
Button4.setTitle("33", forState: UIControlState.Normal)
CorrectAnswer = "2"
break
case 3:
QuestionLabel.text = "What is 3 x 2?"
Button1.setTitle("6", forState: UIControlState.Normal)
Button2.setTitle("8", forState: UIControlState.Normal)
Button3.setTitle("9", forState: UIControlState.Normal)
Button4.setTitle("13", forState: UIControlState.Normal)
CorrectAnswer = "1"
break
case 4:
QuestionLabel.text = "What is 4 x 3?"
Button1.setTitle("8", forState: UIControlState.Normal)
Button2.setTitle("9", forState: UIControlState.Normal)
Button3.setTitle("11", forState: UIControlState.Normal)
Button4.setTitle("12", forState: UIControlState.Normal)
CorrectAnswer = "4"
break
default:
break
}
最简单的方法是使用一个数组来存储所有先前提出的问题的编号。但是,使用随机数生成器生成下一个问题编号并不是一个很好的选择。想想如果你一共问了30个问题,你已经问了29个问题。你将有一个29个数字的数组,因此只剩下一个有效数字,你必须不断地随机选取数字,直到找到数组中不存在的最后一个数字
因此,一个更好的解决方案是列出所有的问题编号,然后重新排列该列表。假设您有10个问题,那么您可以从如下数组开始:
[1,2,3,4,5,6,7,8,9,10]
。接下来,选择一个介于1和10之间的数字,并将数组中的第一个项与该索引处的项交换。例如,如果选择7,则将第一个元素与第七个元素交换:[7、2、3、4、5、6、1、8、9、10]
。对数组中的每个位置重复此操作,以便最终得到如下结果:[3,9,1,7,10,4,2,5,6,8]
。现在,您有了一个随机的问题编号列表,并按照数组给出的顺序提问。每个数字只出现一次,因此您知道同一个问题不会出现多次。我认为您应该将随机数存储在一个数组中。然后检查它,如果它不在数组->调用开关else中,请再次调用随机函数。