0

对不起我的英语不好,我是阿根廷人。我有一个问题,我正在制作一个琐事应用程序,从逻辑上讲,它不必重新问同样的问题。我有 30 个问题,这是我的代码:

-(IBAction)randomear{
    random1=arc4random() % 30;
    if (random1 == 0)
    {
        labelpregunta.text = @"Las ovejas pueden nadar?";
    }
    if (random1 == 1)
    {
        labelpregunta.text = @"Con que se tiñe la lana de negro?";
    }
    if (random1 == 2)
    {
        labelpregunta.text = @"De que material es el mejor casco?";
    }
    if (random1 == 3)
    {
        labelpregunta.text = @"Para fabricar lana necesitas 4 _____";
    }
}

我想创建一个 NSArray ,如果有重复的数字,它会再次随机播放。

我该怎么做?

4

3 回答 3

3

您想要的实际上是一个NSMutableArray(因为您将在新值进入时对其进行扩展),并用于-indexOfObject检查先前选择的值。请注意, NSMutableArray 存储类型为 的对象id,而 int 是一个原始类型。您需要将随机值包装在 NSNumber 中,然后才能存储它们。像这样的东西:

//.h
@property (nonatomic, strong) NSMutableArray *previouslySelectedValues;

//.m
-(IBAction)randomear{
    //I suppose this is an int, right?
    //Supongo que esto es un número entero.
    random1=arc4random() % 30;
    //check if the array can find the object.  It internally uses `-isEqual` in a loop for us
    //estamos comprobando si la matriz se puede encontrar el objeto
    if (![previouslySelectedValues indexOfObject:[NSNumber numberWithInt:random1]]) {
        if (random1 == 0)
        {
            labelpregunta.text = @"Las ovejas pueden nadar?";
        }
        if (random1 == 1)
        {
            labelpregunta.text = @"Con que se tiñe la lana de negro?";
        }
        if (random1 == 2)
        {
            labelpregunta.text = @"De que material es el mejor casco?";
        }
        if (random1 == 3)
        {
            labelpregunta.text = @"Para fabricar lana necesitas 4 _____";
        }

        //et cetera/ etcétera

        //add the object because it doesn't exist and we don't want to select it again.
        //Añadir el objeto a la matriz debido a que es nuevo
        [previouslySelectedValues addObject:[NSNumber numberWithInt:random1]];
    }
    else {
        //do nothing, or use the below pick again if you want
        //No hacer nada, o utilizar el método de abajo para elegir otro número

        //[self randomear];
        return;
    }
}
于 2012-07-18T18:39:39.053 回答
1
    // You store your strings here
    static NSArray *myQuestions = [[NSArray alloc] initWithObjects:
                                          @"Las ovejas pueden nadar?",
                                          @"Con que se tiñe la lana de negro?",
                                          @"De que material es el mejor casco?",
                                          @"Para fabricar lana necesitas 4 _____",nil];
    // Make a copy which is mutable
    NSMutableArray *copy = [NSMutableArray arrayWithArray:myQuestions];
    ...

-(IBAction)randomear{

    // Now select one entry
    random1=arc4random() % [copy count];
    labelpregunta.text = [copy objectAtIndex:random1];
    [copy removeObjectAtIndex:random1];

}
于 2012-07-18T22:58:23.697 回答
0

我不会生成一个随机数并检查该数字是否已被使用,而是创建一个数字 0 到 29 的 NSMutableArray(每个都包含在一个 NSNumber 中),然后使用 Gregory Goltsov 在此 SO 中提供的类别随机打乱数组质疑什么是最好的方式来洗牌一个 nsmutablearray

然后,您只需从 NSMutable 数组的开头迭代每个 NSNumber 对象。IE

#import "NSMutableArray_Shuffling.h // From Gregory Goltsov
NSMutableArray* randomNumbers = [[NSMutableArray alloc] init];
for(int i=0; i<30; i++) {
  [randomNumbers addObject:[NSNumber numberWithInt:i]];
}
[randomNumbers shuffle]; // From Gregory Goltsov

...

int lastIndex = 0;

-(IBAction)randomear
{
    if (lastIndex<30) {
        NSNumber* theRandomNumber = [randomNumbers objectAtIndex:lastIndex];
        int theQuestion = [theRandomNumber intValue];
        if (theQuestion == 0)  {
            labelpregunta.text = @"Las ovejas pueden nadar?";
        }
        if (theQuestion == 1) {
            labelpregunta.text = @"Con que se tiñe la lana de negro?";
        }
        if (theQuestion == 2) {
            labelpregunta.text = @"De que material es el mejor casco?";
        }
        if (theQuestion == 3){
            labelpregunta.text = @"Para fabricar lana necesitas 4 _____";
        }

        //et cetera/ etcétera    
        lastIndex++;
    } else {
        // No more questions
    }
}

但是,最好用一系列对象填充数组,这些对象既包含问题又包含单个问题的答案。IE

@interface aQuestion : NSObject
@property (nonatomic, string) NSString* question;
@property (nonatomic, string) NSString* answer;
-(void)initWithQuestion:(NSString)aQuestion and:(NSString) anAnswer;
-(BOOL)isCorrectAnswer(NSString testAnswer);
@end

@implementation aQuestion
-(void)initWithQuestion:(NSString*)aQuestion and:(NSString*) anAnswer
{
  if(!(self=[super init])) return self;

  question = aQuestion;
  answer = anAnswer;

  return self;
}

-(BOOL)isCorrectAnswer(NSString testAnswer)
{
  [answer isEqualToString:testAnswer];
}
@end

...
#import "NSMutableArray_Shuffling.h // From Gregory Goltsov
NSMutableArray* questions = [[NSMutableArray alloc] init];

[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 1" and:@"Answer 1"]];
[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 2" and:@"Answer 2"]];
[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 3" and:@"Answer 3"]];
[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 4" and:@"Answer 4"]];
[questions shuffle]; // From Gregory Goltsov

...
for(int i=0; i<[questions count]; i++) {
   aQuestion* theQuestion = [questions objectAtIndex:i];

   // Ask theQuestion.question
   labelpregunta.text = theQuestion.question;

   ...

   // wait for theAnswer

   ....

   NSString theAnswer = labelrespuesta.text;

   if ([theQuestion isCorrectAnswer:theAnswer]) {
      // You win!!!!!!!
   }
}

// No more questions

编辑

我最初说的是 Kristopher Johnson 的回答,但我的意思是 Gregory Goltsov 的回答

(Y mi español es peor que el Inglés)

于 2012-07-18T19:19:43.713 回答