我是初学者,几乎没有编程知识,但我正在帮助我11岁的儿子创建一个小测验应用程序,我们遇到了一个大问题,因为我们无法在每个问题中提出问题在所有问题出现之前不要重复的类别。 请在下面找到我们目前的情况(灵感来自我们在网上找到的一个例子),如果有可能,如果有一个简单的解决方案不需要很好的xcode经验来实现它,我将非常感谢您对此问题的帮助。
先谢谢,当然请告诉我这个问题是否需要进一步澄清:
-(void)Category2{
switch (QuestionSelected) {
case 0:
QuestionText.text = [NSString stringWithFormat:@"question1?"];
[Answer1 setTitle:@"A1" forState:UIControlStateNormal];
[Answer2 setTitle:@"A2" forState:UIControlStateNormal];
[Answer3 setTitle:@"A3" forState:UIControlStateNormal];
Answer3Correct = YES;
break;
case 1:
QuestionText.text = [NSString stringWithFormat:@"question2"];
[Answer1 setTitle:@"A1" forState:UIControlStateNormal];
[Answer2 setTitle:@"A2" forState:UIControlStateNormal];
[Answer3 setTitle:@"A3" forState:UIControlStateNormal];
Answer2Correct = YES;
break;
case 2:
QuestionText.text = [NSString stringWithFormat:@"question3"];
[Answer1 setTitle:@"A1" forState:UIControlStateNormal];
[Answer2 setTitle:@"A2" forState:UIControlStateNormal];
[Answer3 setTitle:@"A3" forState:UIControlStateNormal];
Answer1Correct = YES;
break;
default:
break;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
if (GameInProgress == NO) {
LivesNumber = 3;
ScoreNumber = 0;
GameInProgress = YES;
}
Result.hidden = YES;
Exit.hidden = YES;
StartOver.hidden = YES;
Lives.text = [NSString stringWithFormat:@"%i", LivesNumber];
Score.text = [NSString stringWithFormat:@"%i", ScoreNumber];
Answer1Correct = NO;
Answer2Correct = NO;
Answer3Correct = NO;
LevelLoaded = [[NSUserDefaults standardUserDefaults] integerForKey:@"LevelSaved"];
QuestionSelected = arc4random_uniform(5);
附录(2014年7月4日)
请原谅我,因为我们是网站的新手,我们已经在下面的评论中回复了PJS他的答案,我相信格式不清楚。请在下面再次找到我们项目当前状态的副本,希望他能看到出现了什么问题(重复项仍然出现):
LevelLoaded = [[NSUserDefaults standardUserDefaults] integerForKey:@"LevelSaved"];
int length = 20;
int myArray [] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
for (int i = 0; i < length - 1; ++i) {
int j = arc4random_uniform(length - i) + i;
int tmp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = tmp;
}
for (int i = 0; i < length; ++i) {
QuestionSelected = myArray[i];
}
switch (LevelLoaded) {
case 1:
LevelSelected.text = [NSString stringWithFormat:@"Beginner"];
[self Category1];
break;
case 2:
LevelSelected.text = [NSString stringWithFormat:@"Intermediate"];
[self Category2];
break;
case 3:
LevelSelected.text = [NSString stringWithFormat:@"Expert"];
[self Category3];
break;
default:
break;
[super viewDidLoad];
Adendum 2(10.07.2014)
请在下面找到整个m文件: (此处随机仅适用于类别1) 这是你在FOR循环中的SWITCH BLOCK的意思吗? 它仍在继续重复这些问题。我把文字定位错了吗?
#import "Game.h"
@interface Game ()
@end
@implementation Game
-(void)RightAnswer{
ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:@"%i", ScoreNumber];
StartOver.hidden = NO;
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
QuestionText.hidden = YES;
Result.hidden = NO;
Result.image = [UIImage imageNamed:@"rightanswer.jpg"];
}
-(void)WrongAnswer{
LivesNumber = LivesNumber - 1;
Lives.text = [NSString stringWithFormat:@"%i", LivesNumber];
StartOver.hidden = NO;
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
QuestionText.hidden = YES;
Result.hidden = NO;
Result.image = [UIImage imageNamed:@"wronganswer.jpg"];
if (LivesNumber == 0) {
Result.image = [UIImage imageNamed:@"gameover.jpg"];
StartOver.hidden = YES;
Exit.hidden = NO;
GameInProgress = NO;
}
}
-(IBAction)Answer1:(id)sender{
if (Answer1Correct == YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
}
-(IBAction)Answer2:(id)sender{
if (Answer2Correct == YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
}
-(IBAction)Answer3:(id)sender{
if (Answer3Correct == YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
}
-(void)Category1{
int length = 3;
int myArray [] = {0,1,2};
for (int i = 0; i < length - 1; ++i) {
int j = arc4random_uniform(length - i) + i;
int tmp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = tmp;
}
for (int i = 0; i < length; ++i) {
QuestionSelected = myArray[i];
}
switch (QuestionSelected) {
case 0:
QuestionText.text = [NSString stringWithFormat:@"question1"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer3Correct = YES;
break;
case 1:
QuestionText.text = [NSString stringWithFormat:@"question2"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer1Correct = YES;
break;
case 2:
QuestionText.text = [NSString stringWithFormat:@"question3"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer1Correct = YES;
break;
default:
break;
}
}
-(void)Category2{
switch (QuestionSelected) {
case 0:
QuestionText.text = [NSString stringWithFormat:@"question1"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer3Correct = YES;
break;
case 1:
QuestionText.text = [NSString stringWithFormat:@"question2"];
[Answer1 setTitle:@"T.Cubillas(PER)" forState:UIControlStateNormal];
[Answer2 setTitle:@"R.Rensenbrink(NET)" forState:UIControlStateNormal];
[Answer3 setTitle:@"J.Jordan(SCO)" forState:UIControlStateNormal];
Answer2Correct = YES;
break;
case 2:
QuestionText.text = [NSString stringWithFormat:@"question3"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer1Correct = YES;
break;
default:
break;
}
}
-(void)Category3{
switch (QuestionSelected) {
case 0:
QuestionText.text = [NSString stringWithFormat:@"question1"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer2Correct = YES;
break;
case 1:
QuestionText.text = [NSString stringWithFormat:@"question2"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer2Correct = YES;
break;
case 2:
QuestionText.text = [NSString stringWithFormat:@"question3"];
[Answer1 setTitle:@"answer1" forState:UIControlStateNormal];
[Answer2 setTitle:@"answer2" forState:UIControlStateNormal];
[Answer3 setTitle:@"answer3" forState:UIControlStateNormal];
Answer3Correct = YES;
break;
default:
break;
}
}
- (void)viewDidLoad
{
if (GameInProgress == NO) {
LivesNumber = 3;
ScoreNumber = 0;
GameInProgress = YES;
}
Result.hidden = YES;
Exit.hidden = YES;
StartOver.hidden = YES;
Lives.text = [NSString stringWithFormat:@"%i", LivesNumber];
Score.text = [NSString stringWithFormat:@"%i", ScoreNumber];
Answer1Correct = NO;
Answer2Correct = NO;
Answer3Correct = NO;
LevelLoaded = [[NSUserDefaults standardUserDefaults] integerForKey:@"LevelSaved"];
switch (LevelLoaded) {
case 1:
LevelSelected.text = [NSString stringWithFormat:@"Beginner"];
[self Category1];
break;
case 2:
LevelSelected.text = [NSString stringWithFormat:@"Intermediate"];
[self Category2];
break;
case 3:
LevelSelected.text = [NSString stringWithFormat:@"Expert"];
[self Category3];
break;
default:
break;
[super viewDidLoad];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
答案 0 :(得分:1)
在您列举所有可能性之前,语句QuestionSelected = arc4random_uniform(5);
可以(通常会)生成重复值。这是随机性的本质。你应该做的是创建一个问号{0,1,2,3,4}
的数组并对数组进行洗牌。然后遍历洗牌数组,该数组将以随机顺序枚举所有问题而不重复。维基百科有一篇关于Fisher-Yates shuffling algorithm的好文章。调整n
值是有效的,只需要O(n)
工作。
<强>附录强>
你可以构造和改组一个C数组,NSArray对于少数几个整数来说是过度的:
/* Create an array of 5 indices */
int length = 5;
int myArray[] = {0,1,2,3,4};
/* This loop performs a Fisher-Yates shuffle. */
/* Could be implemented as a function or method */
/* for clarity or encapsulation purposes. */
for (int i = 0; i < length - 1; ++i) {
int j = arc4random_uniform(length - i) + i;
int tmp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = tmp;
}
现在用以下内容替换您的语句QuestionSelected = arc4random_uniform(5);
,该语句可以处理重复项:
for (int i = 0; i < length; ++i) {
QuestionSelected = myArray[i];
// do whatever the heck you want with QuestionSelected
}
循环中的每次迭代都会以随机顺序给出一个不同的问题,没有重复。