-1

I have a quiz look-a-like app. It shows up all questions often. How can I make them show up only 1 time, and so after last question, a message will pop up?

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
    }
}
Torre Yan
  • 223
  • 3
  • 9

4 Answers4

0

Just put the inside the case differently:

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
        }
    }
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

Perhaps you could store the questions as an array. Then created a new array of the mixed up questions. Then run through the array presenting the questions to the user. Once you've got to the last question in the array you can display your pop-up message. Here's an example:

1. Shuffle the array, from How do I shuffle an array in Swift?

extension Array {
    func shuffled() -> [T] {
        var list = self
        for i in 0..<(list.count - 1) {
            let j = Int(arc4random_uniform(UInt32(list.count - i))) + i
            swap(&list[i], &list[j])
        }
        return list
    }
}

2. Your questions:

let questions = ["Question1", "Question2", "Question3", "Question4"]
let shuffledQuestions = questions.shuffled()

3. Enumerate through your shuffled questions:

var questionIndex = 0

for question in shuffledQuestions {
    //  Present the question to the user
    self.textLabel.text = question
    questionIndex++
}

4. Once the user has reached the last question present the pop-up. E.g. when questionIndex == shuffledQuestions.count

Community
  • 1
  • 1
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
0

Define a mutable array. Add randomNumber to the array. Check randomNumber is exist in array. If its exist reproduce a random number.

This is your question in objective - 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];
    }
kordiseps
  • 391
  • 3
  • 12
  • The problem with this approach is as you add more an more numbers to your array, the probability of choosing a number that hasn't already been chosen decreases. That's not so much of a problem when there are only five questions. But if there were many more this could end up taking quite a while to create an order for the questions. – ABakerSmith Apr 22 '15 at 13:19
  • if you don't want ask only 5 question, you can create one more array for questions. And bring the switch part to a loop. Like: //This will add random number to asked questions and will ask the question of random number else{ [asked addObject:[NSString stringWithFormat:@"%i",randomNumber]]; self.myLabel.text = [questions objectAtIndex:randomNumber]; } – kordiseps Apr 22 '15 at 13:38
0

Why don't you just put the questions in an array and pop from it every time you show a question? So you can do something like this:

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")
}
Vasil Garov
  • 4,851
  • 1
  • 26
  • 37