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までの数値(それぞれNSNumberでラップされている)のNSMutableArrayを作成し、このSOでGregoryGoltsovによって提供されるカテゴリを使用して配列をランダムにシャッフルしますnsmutablearrayをシャッフルするための最良の方法を質問します。

次に、NSMutable配列の先頭から各NSNumberオブジェクトを繰り返し処理します。すなわち

#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
    }
}

ただし、単一の質問に対する質問と回答の両方を含む一連のオブジェクトで配列を埋める方がよい場合があります。すなわち

@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

編集

私は最初にクリストファー・ジョンソンの答えを言いましたが、私は本当にグレゴリー・ゴルツォフの答えを意味しました

(Ymiespañolespeor queelInglés)

于 2012-07-18T19:19:43.713 に答える