你会添加什么来使它仍然选择一个随机数而不是数字3?
- (IBAction)Button3 {
{
int randomviews = rand() % 6;
Label1.text = [NSString stringWithFormat:@"%i", randomviews];
}
答案 0 :(得分:8)
这是另一种方法,只需一次调用rand()
即可。由于您要从您的范围中排除一个数字,请求较小的数字范围,然后将所有生成的数字替换为前一个范围的最高数字:
- (IBAction)Button3 {
{
int randomviews = rand() % 5;
if (randomviews == 3) {
randomviews = 5
}
Label1.text = [NSString stringWithFormat:@"%i", randomviews];
}
答案 1 :(得分:0)
- (IBAction)Button3 {
int randomviews;
do {
randomviews = rand() % 6;
}
while (randomviews == 3);
Label1.text = [NSString stringWithFormat:@"%i", randomviews];
}
如果你想排除零:
- (IBAction)Button3 {
int randomviews;
do {
randomviews = rand() % 6;
}
while (randomviews == 3 || randomviews == 0);
Label1.text = [NSString stringWithFormat:@"%i", randomviews];