how do I make an int variable defined as either this OR that?
To be more specific here is my code
-(IBAction)randomnumber {
int rNumber1 = 开发者_如何学Pythonrand() % 10;
textview1.text=[[NSString alloc] initWithFormat:@"%d", rNumber1];
int rNumber2 = rand() % 10;
textview2.text=[[NSString alloc] initWithFormat:@"%d", rNumber2];
int rNumber3 = rand() % 10;
textview3.text=[[NSString alloc] initWithFormat:@"%d", rNumber3];
int rNumber4 = rand() % 10;
textview4.text=[[NSString alloc] initWithFormat:@"%d", rNumber4];
}
and this is the just of what i want to do with the first int...
NSArray *keys = [NSArray arrayWithObjects:
@"A" @"B"...
int rNumText1 = rand() % 10 or keys;
textview1.text=[[NSString alloc] initWithFormat:@"%d", rNumText1];
So basically I want a text "Label" to output either a random A-Z or a random 1-9 after the user hits a button on the ipod.
For example I could have 16 of these things and each would randomly chose between outputting a number or outputting a letter, then pick that random letter or number to output like this:
number, letter, letter, number, etc and the next time letter, letter, number, letter, etc
You can do something like so:
int rNumText1;
int random = rand() % 36;
if(random < 10)
rNumText1 = random
else
rNumText1 = keys[random-10];
Or more compactly:
int random = rand() % 36;
int rNumText1 = (random < 10) ? random : key[random-10];
Not entirely sure what you're trying to achieve here, but why not just put the strings "1" to "9" in the array as well and then pick a random number between 0 and 31 (inclusive) and use that as the index to the array?
精彩评论