抱歉我的英语不好,我是阿根廷人。我有一个问题,我是一个琐事应用程序,从逻辑上讲它不需要再问同样的问题。我有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,如果有一个重复的数字,它会再次洗牌。
我该怎么做?
答案 0 :(得分: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;
}
}
答案 1 :(得分: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];
}
答案 2 :(得分:0)
我没有生成一个随机数并检查是否已使用该数字,而是创建一个数字0到29的NSMutableArray(每个包裹在一个NSNumber中),然后随机地重新排列数组使用Gregory Goltsov在此SO问题whats-the-best-way-to-shuffle-an-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)