图片添加到阵列中,随机选择一个,并从数组中删除 [英] Add images to array, pick one at random and delete from array
问题描述
所以,我只是在C#中开始编程(我不知道的基本知识虽然)。跟朋友我有时玩的纸牌游戏,你必须选择一个卡,并根据它什么,我们要做的是一个挑战,失败者必须好好喝一杯。只是为了好玩,我想打这场比赛为Windows窗体应用程序。我在与图像的问题虽然。我想从在程序中可执行文件的目录下的文件夹让他们并将它们添加到一个数组。然后我想随机选择一个(与Random类),单击一个按钮时。一旦卡已被摘下来,我希望它从数组,因此它不能被再次选择删除。
So I just started programming in C# (I do know the basics though). With friends I sometimes play a card game where you have to pick a card and according to what's on it, we have to do a challenge and the loser has to take a drink. Just for fun, I want to make this game as a Windows Form Application. I'm having issues with the images though. I want to get them from a folder that's in the directory of the program executable and add them to an array. Then I want to randomly select one (with the Random class) when a button is clicked. Once a card has been picked, I want it to be deleted from the array so it can't be chosen again.
我一直在寻找通过相当多的SO等地的教程,但我无法找到的东西,完全有效。 <一href=\"http://stackoverflow.com/questions/11320293/shuffle-random-picture-from-resources-on-button-click\">This 之一最接近,因为它没有选择随机照片被点击一个按钮时,并显示它,但我没有找到一个方法来删除该数组的画面。如何添加的功能,从阵列中删除图像?
I've been looking through quite a bit of tutorials on SO and other places, but I can't find something that fully works. This one came closest, as it did select a random picture when a button was clicked and displayed it, but I didn't find a way to remove that picture from the array. How can I add the functionality to delete the image from the array?
编辑:我应该补充说,我已经尝试了一些东西,没有工作。我删除code,虽然(因为它没有工作),但我会再次尝试一些东西,并张贴回来时,我不能让它工作。
I should've added that I've already tried some stuff that didn't work. I deleted that code though ('cause it didn't work), but I'll try some stuff again and post back when I can't make it work.
推荐答案
根据您的示例的链接,我会假设你的code重新present图像作为字符串。
According to the link in your example i will assume that in your code you represent an image as string.
只需使用一个List 列表与LT;字符串&GT;照片=新的List&LT;串GT;();
来牵你的图片
Just use a list List<string> images = new List<string>();
to hold your images
添加列表中的使用元素 images.Add(elemenet)
,其中元素
的类型为字符串
to add elements in the list use images.Add(elemenet)
where element
is of type string
绘制randrom卡的使用
to draw a randrom card use
Randrom randomNumber = new Random();
int extractedCard = randomNumber.Next(images.Length);
然后显示图像[extractedCard]
然后你可以轻松地从列表中删除该卡 images.RemoveAt(extractedCard)
and then you can easily remove that card from the list images.RemoveAt(extractedCard)
这篇关于图片添加到阵列中,随机选择一个,并从数组中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!