【发布时间】:2013-10-28 17:58:39
【问题描述】:
我想知道如何做到这一点,这样同一张图片就不会连续选择两次。可以说图片的编号为 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
}
}
但似乎没有任何效果。有没有办法创建一个不重复的随机数?
【问题讨论】:
-
我想我知道你想要什么,添加一个iVar,即currentSelection,然后使用if语句,检查当前图片是新选择的图片
标签: ios xcode while-loop