gpt4 book ai didi

c# - 再次单击按钮后,如何阻止图片框中的图片再次随机化?

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:40 25 4
gpt4 key购买 nike

我正在为大学做二十一点作业。我有三个装有卡片的图片盒。我有一个抽奖按钮,最多可以抽三张牌。但每次我按下按钮时,卡片都会再次随机化。我把它设置成一次抽一张牌。我点击了一张卡片,但是当我点击了第二张卡片时,第一张卡片再次随机化,第二张和第三张卡片也是如此。

代码:

int count = 0;

private void BtnDraw_Click(object sender, EventArgs e)
{
int Card3;
int Card4;
int Card5;


Random randomizer = new Random();

//get random numbers
Card3 = randomizer.Next(1, 53);
Card4 = randomizer.Next(1, 53);
Card5 = randomizer.Next(1, 53);

// Display the card
PicCard3.Image = imageList1.Images[Card3];
PicCard4.Image = imageList1.Images[Card4];
PicCard5.Image = imageList1.Images[Card5];

这是一次显示一张卡片的代码:

if (count == 0)
{
//Make PictureBox1 visible
PicCard3.Visible = true;
count++;


}
else if (count == 1)
{
//Make PictureBox visible
PicCard4.Visible = true;
count++;



}
else if (count == 2)
{
//Make PictureBox3 visible
PicCard5.Visible = true;
count++;
}
else if (count == 3)
{
//Hide all PictureBoxes again
PicCard3.Visible = false;
PicCard4.Visible = false;
PicCard5.Visible = false;
count = 0;
}

最佳答案

需要很少的逻辑更改:

  • 整个操作应该基于count的值
  • 不需要单独的随机数
  • randomizer 不应该是局部变量

简而言之,您的代码应该是这样的:

Random randomizer = new Random();
int count = 0;
private void BtnDraw_Click(object sender, EventArgs e)
{
int randomCardIndex = randomizer.Next(0,53);
if (count == 0)
{
PicCard3.Image = imageList1.Images[randomCardIndex];
PicCard3.Visible = true;
}
else if (count == 1)
{
PicCard4.Image = imageList1.Images[randomCardIndex];
PicCard4.Visible = true;
}
else if (count == 2)
{
PicCard5.Image = imageList1.Images[randomCardIndex];
PicCard5.Visible = true;
}
else if (count == 3)
{
PicCard3.Visible = false;
PicCard4.Visible = false;
PicCard5.Visible = false;
count = -1;
}
count++;
}

请注意:

如果一次只需要显示一张卡片,则让所有其他卡片的.Visible = false;都在匹配条件下

关于c# - 再次单击按钮后,如何阻止图片框中的图片再次随机化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48149502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com