随机 IBAction,每个案例仅选择一次

发布于 2024-11-26 10:52:10 字数 683 浏览 1 评论 0原文

我在制作 IBAction 时遇到问题,该 IBAction 以随机顺序调用每个案例一次。
我做了很多搜索,但没有找到解决这个问题的好的解决方案。 到目前为止我拥有的代码

-(IBAction) random {  
    int text = rand() % 5;  
    switch (text) {         
        case 0:  
            textview.text = @"1";  
            break;  
        case 1:  
            textview.text = @"2";  
            break;  
        case 2:  
            textview.text = @"3";  
            break;  
        case 3:  
            textview.text = @"4";  
            break;  
        case 4:  
            textview.text = @"5";  
            break;  
        default:  
            break;  
    }      
}

工作正常,但就像我说的,我只希望它调用每个案例一次。
任何帮助将不胜感激。

I am having a problem making a IBAction that calls each case once in a random order.
I have done a lot of searching and not find a good solution to go about this problem.
The code I have so far is

-(IBAction) random {  
    int text = rand() % 5;  
    switch (text) {         
        case 0:  
            textview.text = @"1";  
            break;  
        case 1:  
            textview.text = @"2";  
            break;  
        case 2:  
            textview.text = @"3";  
            break;  
        case 3:  
            textview.text = @"4";  
            break;  
        case 4:  
            textview.text = @"5";  
            break;  
        default:  
            break;  
    }      
}

It works fine, but like I said, I only want it to call each case once.
Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥醉 2024-12-03 10:52:10

由于您正在尝试设置随机文本,因此我会这样做:

添加一个名为 stringsArray 之类的 iVar 并在 init 上初始化它:

...
stringsArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
...

您的 IBAction 将如下所示:

-(IBAction) random {  
    if([stringsArray count] == 0) return;
    int text = rand() % [stringsArray count];  
    [textView setText:[stringsArray objectAtIndex:text];
    [stringsArray removeObjectAtIndex:text];
}

Since you are trying to set a random text, I'd do it like this:

Add an iVar called something like stringsArray and initialize it on your init:

...
stringsArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
...

Your IBAction would look like this:

-(IBAction) random {  
    if([stringsArray count] == 0) return;
    int text = rand() % [stringsArray count];  
    [textView setText:[stringsArray objectAtIndex:text];
    [stringsArray removeObjectAtIndex:text];
}
你是我的挚爱i 2024-12-03 10:52:10

将选项(如 NSNumber )放入 NSMutableArray 中,选择一个随机索引,显示它,然后将其从数组中删除......如果有其他一些响应,怎么样?计数达到零。

What about putting the options, as NSNumbers, in an NSMutableArray, selecting a random index, display it, and remove it from the array...with some other response if the count reaches zero.

初熏 2024-12-03 10:52:10

使用 srand 代替 rand 函数

或者最好还是使用

int text = arc4random() % 5;

Use srand instead of rand function

Or Better still use

int text = arc4random() % 5;

谈下烟灰 2024-12-03 10:52:10

好吧,不管怎样,您需要保留一张“记分卡”并跳过已经选择的选项。记分卡可以是位图(例如,设置位的简单整数)、布尔数组、先前选择的数字列表等。

最直接的编码方法是拥有逻辑主体上面的 UNTIL 循环中,在选择随机数后检查循环,看看它是否已经被选择。如果是这样,则在循环中进行迭代,否则设置您的响应,在记分卡中填写适当的“复选标记”,然后离开循环。

但要注意:循环还必须包括检查是否已选择所有选项,并在这种情况下退出并带有一些适当的指示。

Well, one way or another you need to keep a "scorecard" and skip options that have already been selected. The scorecard can be a bit map (eg, a simple integer where you set bits), an array of booleans, a list of previously chosen numbers, etc.

The most straight-forward way to code it is to have the body of your logic above in an UNTIL loop and check in the loop, after picking the random number, to see if it's already chosen. If so, iterate in the loop, otherwise set your response, fill in the appropriate "checkmark" in your scorecard, and LEAVE the loop.

But BEWARE: The loop must also include a check to see if all options have already been chosen, and exit with some appropriate indication in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文