- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想使用 C# 创建纸牌魔术游戏。我将表格上的图片框设计为卡片(背面)。我还为每张图片创建了一个 Click 方法,该方法创建一个介于 0 和 51 之间的随机数,并使用该数字从 ImageList 设置图像。
Random random = new Random();
int i = random.Next(0, 51);
pictureBox1.Image = imageList1.Images[i];
我的问题是有时我会得到相同的数字(例如:两个黑桃 J),我该如何防止这种情况发生?! (我的意思是,例如,如果我得到(5),我可能会得到另一个(5))
最佳答案
将您已经选择的号码存储在 HashSet<int>
中并继续选择,直到当前号码不在 HashSet
中:
// private HashSet<int> seen = new HashSet<int>();
// private Random random = new Random();
if (seen.Count == imageList1.Images.Count)
{
// no cards left...
}
int card = random.Next(0, imageList1.Images.Count);
while (!seen.Add(card))
{
card = random.Next(0, imageList1.Images.Count);
}
pictureBox1.Image = imageList1.Images[card];
或者,如果您需要选择多个数字,您可以用序列号填充一个数组,并将每个索引中的数字与另一个随机索引中的数字交换。然后从随机数组中取出前 N 个需要的项目。
关于c# - 使用 C# 设计 Card Trick Game 的一个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637175/
trick大意 我对于这个trick的理解为:支持位运算的高精度 维护一个以 \(b\) 为基数的大数 \(N\) ,并支持以下功能: 给定(可能是负)整数 \(|x|
我正在为 Java 考试进行培训,我在去年的科目中遇到了一些我不理解的东西。这是代码 class Mother { int var = 2; int getVar() {
在一点帮助下,它几乎是华丽的:TheZoo.com/shapes 除此之外,标题图标和我想要控制的文本之间没有空格。那么,如何在 css pseudo:before 形状中添加边距(或填充),同时避免
是否有一些使用媒体查询的新标准。或者我应该说,应该做出一些新的考虑。 CSS tricks article似乎效果不是很好,因为“智能手机景观”似乎劫持了一切。 我直接使用的代码(打印屏幕应该是什么除
我有一个名为 memory_region 的类,有点像未类型化的 gsl::span (即它本质上是一个 void* 和一个 size_t ),我也将其用于类型删除。因此它有一个 as_span()方
在阅读了 Douglas Crockford 的“JavaScript:The Good Parts”和 Stoyan Stevanov 的“JavaScript Patterns”之后,我试图确定“
最近我发现,如果我需要查看变量是否为偶数(或奇数),我可以只查看变量的最后一位是否等于 0。这一发现在实现后取代了一些 modulo 2 计算,因此整个函数运行得更快。 是否有任何其他像这个一样的“技
我一直在玩这个,到目前为止我还没有找到任何方法来隐藏或欺骗 instanceof 通过隐藏它的类型来返回 false混淆层,但这并不意味着它是不可能的,因为我远不是最了解 Java 的人。所以才来请教
我知道特征散列 (hashing-trick) 用于降低维度和处理位向量的稀疏性,但我不明白它是如何工作的。谁能给我解释一下。是否有任何 python 库可用于进行特征散列? 谢谢。 最佳答案 在 P
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在查看“dlib”库,更具体地说是用于“优化”的“find_min”函数( http://dlib.net/optimization_ex.cpp.html)。 “find_min”函数可让您将自
有据可查的是,IE 中的原生 DOM 元素不包含 hasOwnProperty() 方法。有几个解决方案;其中最优雅的是直接在 Object.prototype 中访问 hasOwnProperty(
This question状态: It is amazing how many users don't know about the rm ./-rf or rm -- -rf tricks. 我不敢
我已经提到了 this site用于布局技巧。 在那里,一个属性被声明为 android:layout_weight="1"。 所以我的困惑是关于 android:layout_weight: and
我正在尝试使一个元素垂直居中,该元素使用 padding-bottom 来创建所需的宽高比(对于视频)。我想要顶部和底部的黑色边框,就像电影以比拍摄时更高的纵横比显示时一样: body{ pa
你能解释一下下一个代码示例背后的机制吗(我想我知道,但我需要第二个意见): 1)---------------------------- using namespace std; int * f(in
滚动浏览 http://git.suckless.org/st/plain/st.c我偶然发现 #define LEN(a) (sizeof(a) / sizeof(a)[0]) 我知道预编译魔法很难
我正在阅读有关 Java 泛型的文章,我偶然发现了这个主题,我对此感到有些困惑。 发件人:http://www.angelikalanger.com/GenericsFAQ/FAQSections/P
我有一点挑战。 在我们产品的早期版本中,我们有一个错误消息窗口(不得已,未处理的异常)显示异常消息、类型、堆栈跟踪 + 各种信息。 这个窗口是打印屏幕友好的,因为如果用户简单地进行打印屏幕捕获,并将屏
可以在 C 中将某些类型的泛型函数作为宏来执行,例如: #define SQRT(x) (sizeof(x) == sizeof(float) ? sqrtf((x)) : \
我是一名优秀的程序员,十分优秀!