Xcode 测验也有同样的问题
我有一个类似测验的应用程序。它经常显示所有的问题。我怎样才能让他们只出现一次,这样在最后一个问题之后,就会弹出一条消息Xcode 测验也有同样的问题,xcode,swift,arc4random,Xcode,Swift,Arc4random,我有一个类似测验的应用程序。它经常显示所有的问题。我怎样才能让他们只出现一次,这样在最后一个问题之后,就会弹出一条消息 func nextText(){ let randomNumber = Int(arc4random_uniform(4)) var textLabel = "" as NSString switch (randomNumber){ case 1: textLabel = "Question
func nextText(){
let randomNumber = Int(arc4random_uniform(4))
var textLabel = "" as NSString
switch (randomNumber){
case 1:
textLabel = "Question1."
break
case 2:
textLabel = "Question2."
break
case 3:
textLabel = "Question3."
break
case 4:
textLabel = "Question4."
break
default:
textLabel = "Question 5"
}
self.textLabel.text = textLabel as? String
}
}
只需在箱子里换一种方式:
func nextText(){
let randomNumber = Int(arc4random_uniform(4))
var textLabel = "" as NSString
switch (randomNumber){
case 1:
textLabel = "Question1."
self.textLabel.text = textLabel as? String
break
case 2:
textLabel = "Question2."
self.textLabel.text = textLabel as? String
break
case 3:
textLabel = "Question3."
self.textLabel.text = textLabel as? String
break
case 4:
textLabel = "Question4."
self.textLabel.text = textLabel as? String
break
default:
textLabel = "Question 5"
self.textLabel.text = textLabel as? String
}
}
也许您可以将问题存储为一个数组。然后创建了一系列新的混合问题。然后运行数组,将问题呈现给用户。在数组中找到最后一个问题后,可以显示弹出消息。下面是一个例子: 1。从 3.列举你的问题:
let questions = ["Question1", "Question2", "Question3", "Question4"]
let shuffledQuestions = questions.shuffled()
var questionIndex = 0
for question in shuffledQuestions {
// Present the question to the user
self.textLabel.text = question
questionIndex++
}
4.用户完成最后一个问题后,弹出窗口。例如,当
questionIndex==shuffledQuestions.count
定义一个可变数组时。将随机数添加到数组中。检查数组中是否存在随机数。如果其存在,则复制一个随机数
这是你在目标c中的问题:
//Defining an array
@property NSMutableArray *asked;
-(void) nextText{
//This will generate a random number between 1 and 5.
int randomNumber = arc4random() % 5+1;
NSString * textLabel =nil;
//This will check all questions are asked
if ([asked count] ==5) {
self.myLabel.text = @"All questions completed";
return;
}
//This will check is random number asked
if ([asked containsObject:[NSString stringWithFormat:@"%i",randomNumber]]) {
return;
}
//This will add random number to asked questions and will ask the question of random number
else{
[asked addObject:[NSString stringWithFormat:@"%i",randomNumber]];
switch (randomNumber){
case 1:
textLabel = @"Question1.";
break;
case 2:
textLabel = @"Question2.";
break;
case 3:
textLabel = @"Question3.";
break;
case 4:
textLabel = @"Question4.";
break;
case 5:
textLabel = @"Question5.";
break;
}
self.myLabel.text = textLabel;
}
}
//If you want more than limited question, create and fill question array
//After you can ask like this. Change else part with this
else{
[asked addObject:[NSString stringWithFormat:@"%i",randomNumber]];
self.myLabel.text = [questions objectAtIndex:randomNumber];
}
你为什么不把问题放在一个数组中,每次你展示一个问题时就从中弹出?所以你可以这样做:
var questions = ["Question1", "Question2", "Question3", "Question4", "Question5"]
for var i = questions.count; i > 0; {
let randomNumber = Int(arc4random_uniform(UInt32(--i)))
let textLabel = questions[randomNumber] as String
println(textLabel)
questions.removeAtIndex(randomNumber)
}
if questions.count == 0 {
println("should show the popup")
}
您所做的只是更改设置
textlab.text
,此代码的结果与问题中的代码完全相同。@ABakerSmith我收到了所有不同的结果。但我认为问题要求不要重复这些问题。除非我误解了。这种方法的问题是,当你向数组中添加更多的数字时,选择一个尚未选择的数字的概率会降低。当只有五个问题时,这不是什么大问题。但是如果有更多的问题,这可能会花费相当长的时间来创建问题的顺序。如果您不想只问5个问题,您可以为问题创建更多的数组。并将开关部分置于回路中。例如://这将向询问的问题添加随机数,并询问随机数的问题{[asked addObject:[NSString stringWithFormat:@“%i”,randomNumber]];self.myLabel.text=[questions objectAtIndex:randomNumber];}
var questions = ["Question1", "Question2", "Question3", "Question4", "Question5"]
for var i = questions.count; i > 0; {
let randomNumber = Int(arc4random_uniform(UInt32(--i)))
let textLabel = questions[randomNumber] as String
println(textLabel)
questions.removeAtIndex(randomNumber)
}
if questions.count == 0 {
println("should show the popup")
}