我正在创建一个包含9个不同数字的随机数游戏,我想将这些数字随机化,以便将它们随机化为非重复数据(例如,1,4,2,3,6,5,9,7,8)但目前我正在获得1到9之间的9个随机数,并且有多个副本。任何人都可以在不改变现有代码的情况下提出任何建议。
谢谢。
-(IBAction)Randomise
{
int rNumber1 = arc4random_uniform(9);
Text1.text = [NSString stringWithFormat: @"%d",rNumber1 + 1];
if ([Text1.text isEqualToString: @"1"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text1.text isEqualToString: @"2"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text1.text isEqualToString: @"3"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text1.text isEqualToString: @"4"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text1.text isEqualToString: @"5"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text1.text isEqualToString: @"6"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text1.text isEqualToString: @"7"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text1.text isEqualToString: @"8"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text1.text isEqualToString: @"9"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber2 = arc4random_uniform(9);
Text2.text = [NSString stringWithFormat: @"%d",rNumber2+1];
if ([Text2.text isEqualToString: @"1"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text2.text isEqualToString: @"2"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text2.text isEqualToString: @"3"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text2.text isEqualToString: @"4"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text2.text isEqualToString: @"5"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text2.text isEqualToString: @"6"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text1.text isEqualToString: @"7"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text1.text isEqualToString: @"8"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text1.text isEqualToString: @"9"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber3 = arc4random_uniform(9);
Text3.text = [NSString stringWithFormat: @"%d",rNumber3+1];
if ([Text3.text isEqualToString: @"1"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text3.text isEqualToString: @"2"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text3.text isEqualToString: @"3"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text3.text isEqualToString: @"4"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text3.text isEqualToString: @"5"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text3.text isEqualToString: @"6"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text3.text isEqualToString: @"7"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text3.text isEqualToString: @"8"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text3.text isEqualToString: @"9"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber4 = arc4random_uniform(9);
Text4.text = [NSString stringWithFormat: @"%d",rNumber4+1];
if ([Text4.text isEqualToString: @"1"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text4.text isEqualToString: @"2"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text4.text isEqualToString: @"3"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text4.text isEqualToString: @"4"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text4.text isEqualToString: @"5"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text4.text isEqualToString: @"6"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text4.text isEqualToString: @"7"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text4.text isEqualToString: @"8"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text4.text isEqualToString: @"9"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber5 = arc4random_uniform(9);
Text5.text = [NSString stringWithFormat: @"%d",rNumber5+1];
if ([Text5.text isEqualToString: @"1"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text5.text isEqualToString: @"2"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text5.text isEqualToString: @"3"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text5.text isEqualToString: @"4"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text5.text isEqualToString: @"5"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text5.text isEqualToString: @"6"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text5.text isEqualToString: @"7"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text5.text isEqualToString: @"8"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text5.text isEqualToString: @"9"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber6 = arc4random_uniform(9);
Text6.text = [NSString stringWithFormat: @"%d",rNumber6+1];
if ([Text6.text isEqualToString: @"1"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text6.text isEqualToString: @"2"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text6.text isEqualToString: @"3"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text6.text isEqualToString: @"4"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text6.text isEqualToString: @"5"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text6.text isEqualToString: @"6"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text6.text isEqualToString: @"7"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text6.text isEqualToString: @"8"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text6.text isEqualToString: @"9"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber7 = arc4random_uniform(9);
Text7.text = [NSString stringWithFormat: @"%d",rNumber7+1];
if ([Text7.text isEqualToString: @"1"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text7.text isEqualToString: @"2"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text7.text isEqualToString: @"3"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text7.text isEqualToString: @"4"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text7.text isEqualToString: @"5"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text7.text isEqualToString: @"6"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text7.text isEqualToString: @"7"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text7.text isEqualToString: @"8"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text7.text isEqualToString: @"9"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber8 = arc4random_uniform(9);
Text8.text = [NSString stringWithFormat: @"%d",rNumber8+1];
if ([Text8.text isEqualToString: @"1"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text8.text isEqualToString: @"2"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text8.text isEqualToString: @"3"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text8.text isEqualToString: @"4"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text8.text isEqualToString: @"5"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text8.text isEqualToString: @"6"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text8.text isEqualToString: @"7"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text8.text isEqualToString: @"8"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text8.text isEqualToString: @"9"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber9 = arc4random_uniform(9);
Text9.text = [NSString stringWithFormat: @"%d",rNumber9+1];
if ([Text9.text isEqualToString: @"1"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text9.text isEqualToString: @"2"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text9.text isEqualToString: @"3"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text9.text isEqualToString: @"4"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text9.text isEqualToString: @"5"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text9.text isEqualToString: @"6"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text9.text isEqualToString: @"7"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text9.text isEqualToString: @"8"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text9.text isEqualToString: @"9"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"9.png"]];
}
AnimatedPicture.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture1.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture3.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture4.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture5.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture6.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture7.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture8.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
[AnimatedPicture setAnimationRepeatCount:3];
AnimatedPicture.animationDuration = 1.0;
[AnimatedPicture startAnimating];
[AnimatedPicture1 setAnimationRepeatCount:3];
AnimatedPicture1.animationDuration = 1.0;
[AnimatedPicture1 startAnimating];
[AnimatedPicture2 setAnimationRepeatCount:3];
AnimatedPicture2.animationDuration = 1.0;
[AnimatedPicture2 startAnimating];
[AnimatedPicture3 setAnimationRepeatCount:3];
AnimatedPicture3.animationDuration = 1.0;
[AnimatedPicture3 startAnimating];
[AnimatedPicture4 setAnimationRepeatCount:3];
AnimatedPicture4.animationDuration = 1.0;
[AnimatedPicture4 startAnimating];
[AnimatedPicture5 setAnimationRepeatCount:3];
AnimatedPicture5.animationDuration = 1.0;
[AnimatedPicture5 startAnimating];
[AnimatedPicture6 setAnimationRepeatCount:3];
AnimatedPicture6.animationDuration = 1.0;
[AnimatedPicture6 startAnimating];
[AnimatedPicture7 setAnimationRepeatCount:3];
AnimatedPicture7.animationDuration = 1.0;
[AnimatedPicture7 startAnimating];
[AnimatedPicture8 setAnimationRepeatCount:3];
AnimatedPicture8.animationDuration = 1.0;
[AnimatedPicture8 startAnimating];
}
答案 0 :(得分:1)
创建所需数字的数组,然后随机选择两个数字并切换其数组位置。做那个随机切换一百次左右。这个想法基本上就像洗牌一样。