我想知道如何制作它,以免连续两次选择同一张图片。可以说图片的编号为 1-3。如果选择了图片 1,那么接下来不会选择图片 1。如果选择了图片 3,则可以再次选择图片 1,以此类推。
我知道我必须使用一个while
语句,但我不知道如何使用。这是我现在所拥有的:
- (void)chooseBackgroundImage{
if(thisNumber % 10 == 0){
int chooseBackgroundImage = arc4random() % 7;
switch (chooseBackgroundImage) {
case 0:
backgroundImage.image = [UIImage imageNamed:@"CyanToYellowBackground.png"];
break;
case 1:
backgroundImage.image = [UIImage imageNamed:@"GreenToBlueBackground.png"];
break;
case 2:
backgroundImage.image = [UIImage imageNamed:@"OrangeToGreenBackground.png"];
break;
case 3:
backgroundImage.image = [UIImage imageNamed:@"OrangeToPurpleBackground.png"];
break;
case 4:
backgroundImage.image = [UIImage imageNamed:@"PurpleToCyanBackground.png"];
break;
case 5:
backgroundImage.image = [UIImage imageNamed:@"RedToBlueBackground.png"];
break;
case 6:
backgroundImage.image = [UIImage imageNamed:@"YellowToRedBackground.png"];
break;
}
}
}
我也尝试过使用:
- (void)chooseBackgroundImage{
if(slogansGenerated % 10 == 0){
int chooseBackgroundImage = arc4random() % 7;
while(chooseBackgroundImage == oldChooseBackgroundImage){
switch (chooseBackgroundImage) {
case 0:
backgroundImage.image = [UIImage imageNamed:@"CyanToYellowBackground.png"];
break;
case 1:
backgroundImage.image = [UIImage imageNamed:@"GreenToBlueBackground.png"];
break;
case 2:
backgroundImage.image = [UIImage imageNamed:@"OrangeToGreenBackground.png"];
break;
case 3:
backgroundImage.image = [UIImage imageNamed:@"OrangeToPurpleBackground.png"];
break;
case 4:
backgroundImage.image = [UIImage imageNamed:@"PurpleToCyanBackground.png"];
break;
case 5:
backgroundImage.image = [UIImage imageNamed:@"RedToBlueBackground.png"];
break;
case 6:
backgroundImage.image = [UIImage imageNamed:@"YellowToRedBackground.png"];
break;
}
int oldChooseBackgroundImage = chooseBackroundImage
}
}
但似乎没有任何效果。有没有办法创建一个不重复的随机数?