如果列表为空,则跳过随机迭代 [英] Skip a random iteration if the list is null

查看:85
本文介绍了如果列表为空,则跳过随机迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



我有一个随机方法:



Hi.

I have a Random method:

private string Random(Random random)
{
    if (random.Next(animals.Count) && randomAnimal.Name != null)
    {
        txtName.Text = randomAnimal.Name;
        return randomAnimal.Name;
    }
    else if (randomAnimal.Specie != null)
    {
        txtSpecie.Text = randomAnimal.Specie;
        return randomAnimal.Specie;
    }
    else
    {
        return MessageBox.Show("Failure").ToString();
    }
}





我尝试了什么:



你可以看到这基本上显示了一个列表'动物'的随机物品



我也做无论文本框中显示什么,它都被设置为null。



是否可以跳过随机迭代并重新启动Random方法以生成not null文本框中的值?



重点是它有时只将随机项添加到txtName或txtSpecie。例如,如果它显示在txtName中,则会使列表中的txtName值为null。我想确保当生成随机并且下一个随机值为null时,它不会尝试显示,而是重新开始生成随机。



提前谢谢



What I have tried:

As you can see this basically displays a random item from a List 'animals'

I also make that whatever is displayed in the textbox it then is being set to null.

Is it possible to skip random iteration and start the Random method all over again to generate not null values in the text boxes?

The point is that it sometimes add the random item only to txtName or txtSpecie. If, for example it is displayed in txtName it make the txtName value null in the list. I want to make sure that when random is being generated and the next random value is null it will not try to display but instead start to generate the random all over again.

Thanks in advance

推荐答案

为什么不在项目分配后删除它,这样就不需要进行不必要的迭代了。 [ ^ ]
Why not just remove it once that item has been assigned, in this way, there is no need for unnecessary iterations. [^]


这篇关于如果列表为空,则跳过随机迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆